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

  • SEARCH
  • Home
  • 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 6075161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:27:48+00:00 2026-05-23T10:27:48+00:00

I have a REST based WCF web-service; The contract is: [OperationContract] [WebInvoke(Method = POST,

  • 0

I have a REST based WCF web-service;

The contract is:

[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Xml)]
string EchoWithPost(string message);

The message is:

public string EchoWithPost(string s)
{
    return "ECHO with POST : You said " + s;
}

I used the web channel factory to get a response via POST and it works. I used wireshark to tap the message and I can see some important things:

1) That xml is sent
2) The Content Type

From this I have constructed the following request logic:

        //5) manually post to the REST service
        //Create a request using a URL that can receive a post. 
        WebRequest request = WebRequest.Create(urlOfService + "/rest/EchoWithPOST");
        // Set the Method property of the request to POST.
        request.Method = "POST";
        // Create POST data and convert it to a byte array.


        string postData = "<EchoWithPost xmlns="http://tempuri.org"><message>Hello</message><EchoWithPost>";



        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        // Set the ContentType property of the WebRequest.
        request.ContentType = "application/xml";
        // Set the ContentLength property of the WebRequest.
        request.ContentLength = byteArray.Length;
        // Get the request stream.
        Stream dataStream = request.GetRequestStream();
        // Write the data to the request stream.
        dataStream.Write(byteArray, 0, byteArray.Length);
        // Close the Stream object.
        dataStream.Close();
        // Get the response.
        WebResponse response = request.GetResponse();
        // Display the status.
        Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        // Get the stream containing content returned by the server.
        dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();
        // Display the content.
        Console.WriteLine(responseFromServer);
        // Clean up the streams.
        reader.Close();
        dataStream.Close();
        response.Close();

However when I hit the line that says:

dataStream =
response.GetResponseStream();

I get the following error:

“The remote server returned an error : (400) Bad Request”

Could someone help me with what I need to do as I need to be able to tell people how to manually create a POST request to interact with this REST based service.

Any help much appreciated dont really see what else I can try.

  • 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-23T10:27:49+00:00Added an answer on May 23, 2026 at 10:27 am

    I’ve made a few small changes, so I’ll just post the entire thing. Hopefully it works for you. Also, I didn’t add any deserializing, figuring you could tackle that as long as you make it past the HTTP 400 error.

    A great tool to help you debug these situations is SoapUI. Just setup a “Web TestCase”, and you can create your own POST requests and monitor the data that’s going back and forth.

    -Vito

    Interface:

        [OperationContract]
        [WebInvoke(UriTemplate = "EchoWithPost", Method="POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
        string EchoWithPost(string message);
    

    Service:

        public string EchoWithPost(string s)
        {
            return "ECHO with POST : You said " + s;
        }
    

    Client:

    string urlOfService = "http://somewhere.com/RestService.svc/EchoWithPost";
    string postData = "<EchoWithPost xmlns=\"http://tempuri.org/\"><message>Vito</message></EchoWithPost>";
    byte[] byteArray = Encoding.UTF8.GetBytes(postData);
    
    WebRequest request = WebRequest.Create(urlOfService);
    request.Method = "POST";
    request.ContentType = "application/xml;";
    request.ContentLength = byteArray.Length;
    Stream dataStream = request.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);
    dataStream.Close();
    
    WebResponse webResponse = request.GetResponse();
    
    // Output raw string result
    string rawStringResult = new StreamReader(webResponse.GetResponseStream()).ReadToEnd();
    HttpContext.Current.Response.Write("\r\n" + rawStringResult);
    

    web.config:

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a self-hosted WCF REST service based on the WebHttpBinding. One of the
I have a WCF REST service that exposes a couple dozen objects and based
I have a WCF REST Web Service which returns POCO entities generated by Entity
I have created a rest architecture based web service in C# which will return
I have a WCF/REST Web Service that I'm trying to add a global exception
I have a WCF 4.0 REST service. If I enable automaticFormatSelectionEnabled in the web.config
I have a WCF REST 4.0 project based on the the WCF REST Service
am new to Json so a little green. I have a Rest Based Service
I have some REST web services implemented in WCF. I wish to make these
First a little bit of background: I have a REST service in WCF 4

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.