tags:

views:

684

answers:

4

Can anyone point me to an example how to post a SOAP Request to a WCF Service and return a SOAP Response? Basically a Travel client sends a SOAP request with search parameters and the WCF Service checks within the database and then sends the appropriate holidays.

I keep getting this error, with the method I have used: "The remote server returned an error: (400) Bad Request"

+1  A: 

The error you got is because the server does not understand the HTTP request. It could be the binding you configured or the service proxy is incorrect at client level.

Or the service you defined expects HTTP GET rather than HTTP POST. Sometimes the add service reference may not generate correct HTTP verb for some [WebGet] attributed operations. You may need to add [WebGet] for the operation at client side manually.

codemeit
A: 

You haven't given many details as to how far along you are with the service, so it's hard to say.

If this is literally the first hit to the service, this error could occur if WCF has not been registered properly with IIS. Specifically the .svc extension needs to be mapped to the ASP.NET ISAPI module.

Drew Marsh
A: 

Either have a look at SoapUI, or locate the WcfTestClient buried deep in your Visual Studio folders (C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE).

Both can connect to a WCF service and send/receive SOAP messages.

Or create your own little client, using svcutil.exe:

svcutil.exe  (service URL)

will create a little *.cs file and a *.config file for you, which you can then use to call the service.

Marc

marc_s
A: 

hi guys,

thanks for taking the time out to answer this. The service works fine, if a client creates a reference to my WCF Service and makes a method call, the appropriate response is sent.

I forgot to add, that my client is sends a HTTP Post Request to my WCF Service. The appropriate response is then created and returned to the Client.

I can read the HTTP Request, however when i try and access the HTTP response, i get error -"The remote server returned an error: (400) Bad Request"

The error happens when the code reaches this line:

        // Get the response. 
        HttpWebResponse response = request.GetResponse() as HttpWebResponse;

See code below:

 private void CreateMessage()
    {
        // Create a request using a URL that can receive a post. 
        WebRequest request = WebRequest.Create("http://www.XXXX.com/Feeds");
        string postData = "<airport>Heathrow</airport>";

// user function request.Method = "POST";

        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        request.ContentType = "application/soap+xml; charset=utf-8";
        request.ContentLength = byteArray.Length;

        Stream dataStream = request.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();

        // Get the response. 
        HttpWebResponse response = request.GetResponse() as HttpWebResponse;

        // Display the status. 
        HttpContext.Current.Response.Write(((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. 
        HttpContext.Current.Response.Write(responseFromServer);

        // Clean up the streams. 
        reader.Close();
        dataStream.Close();
        response.Close(); 

    }

regards

Kojo

Kojof