Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6101833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:32:41+00:00 2026-05-23T13:32:41+00:00

Can anyone explain to me why im getting a http 400 error when trying

  • 0

Can anyone explain to me why im getting a http 400 error when trying to post to my webservice?

My service contract ::

[ServiceContract]
public interface IfldtWholesaleService {
    [OperationContract]
    [WebInvoke(Method = "POST",
        ResponseFormat = WebMessageFormat.Xml,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "MAC")]
    string MAC(string input);

My call;

    private void postToWebsite()
    {
        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(txtUrl.Text);
        req.Method = "POST";
        req.MediaType = "text/xml";


        string input = "dfwa";
        req.ContentLength = ASCIIEncoding.UTF8.GetByteCount(input);

        StreamWriter writer = new StreamWriter(req.GetRequestStream());
        writer.Write(input);
        writer.Close();
        var rsp = req.GetResponse().GetResponseStream();

        txtOut.Text = new StreamReader(rsp).ReadToEnd();
    }

My server config file

<system.serviceModel>
    <services>
        <service name="fldtRESTWebservice.fldtWholesaleService" behaviorConfiguration="httpBehaviour">
            <endpoint address="" binding="webHttpBinding" contract="fldtRESTWebservice.IfldtWholesaleService" behaviorConfiguration="httpEndpointBehavour">
                <identity>
                    <dns value="localhost"/>
                </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:8080/ContactService/"/>
                </baseAddresses>
            </host>
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="httpBehaviour">
                <serviceMetadata httpGetEnabled="True"/>
                <serviceDebug includeExceptionDetailInFaults="False"/>
            </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="httpEndpointBehavour">
                <webHttp/>
            </behavior>
        </endpointBehaviors>
    </behaviors>
</system.serviceModel>

EDIT :: it also gives the same error when using MediaType “text/plain”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:32:42+00:00Added an answer on May 23, 2026 at 1:32 pm

    An endpoint with webHttpBinding / webHttp behavior by default accepts requests in either XML or JSON format. And the XML you send needs to conform with what the service expects. The code below sends a request which your service expects. Also, notice that you need to set the ContentType property on the HttpWebRequest, not MediaType.

    public class StackOverflow_6550019
    {
        [ServiceContract]
        public interface IfldtWholesaleService
        {
            [OperationContract]
            [WebInvoke(Method = "POST",
                ResponseFormat = WebMessageFormat.Xml,
                BodyStyle = WebMessageBodyStyle.Wrapped,
                UriTemplate = "MAC")]
            string MAC(string input);
        }
    
        public class Service : IfldtWholesaleService
        {
            public string MAC(string input)
            {
                return input;
            }
        }
    
        private static void postToWebsite(string url)
        {
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "text/xml";
    
            string input = @"<MAC xmlns=""http://tempuri.org/""><input>hello</input></MAC>";
    
            StreamWriter writer = new StreamWriter(req.GetRequestStream());
            writer.Write(input);
            writer.Close();
            var rsp = req.GetResponse().GetResponseStream();
    
            Console.WriteLine(new StreamReader(rsp).ReadToEnd());
        }
    
        public static void Test()
        {
            string baseAddress = "http://" + Environment.MachineName + ":8000/Service/";
            WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
            host.Open();
            Console.WriteLine("Host opened");
    
            // To find out the expected request, using a WCF client. Look at what it sends in Fiddler
            var factory = new WebChannelFactory<IfldtWholesaleService>(new Uri(baseAddress));
            var proxy = factory.CreateChannel();
            proxy.MAC("Hello world");
    
            postToWebsite(baseAddress + "/MAC");
    
            Console.Write("Press ENTER to close the host");
            Console.ReadLine();
            host.Close();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Not really getting the point of the map function. Can anyone explain with examples
Can anyone explain what this mod_rewrite rule is doing? I'm trying to comment the
Can anyone explain why the following test is not passing. The regex is getting
Can anyone explain what advantages there are to using a tool like MSBuild (or
Can anyone explain why following code won't compile? At least on g++ 4.2.4. And
Can anyone explain in simple words what First and Second Level caching in Hibernate/NHibernate
Can anyone explain the difference between Server.MapPath(.) , Server.MapPath(~) , Server.MapPath(@\) and Server.MapPath(/) ?
Can anyone explain the meaning of and purposes of the values for the SecurityAction
Can anyone explain to me what this means? Run-Time Check Failure #0 - The
Can anyone explain the differences between Protocols and Categories in Objective-C? When do you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.