views:

32

answers:

1

Hello, I'm trying to dynamically send a SOAP request to different webservices. Each webservice has its own ID, so I just basically have to change the ID of the webservice in the URL, E.G.:

http://mywebservice.com/ID/servicedosomething

Anyway, I don't know how to do this manually. I can't reference the services because I would have to add a lot of web references into the app, which doesn't seem very good to do.

Anyway, I just want to know how to construct the SOAP request, send it, and get the result from the service. Btw, I've checked other solutions to similar questions and none worked for me, might be the WP7 framework or something.

Thanks!

+1  A: 

Hi Carlo,

From my experience, it is very easy to design and build Windows Phone applications with RESTful web services. In a situation where you only have SOAP XML web services to work with, you will need to do some work within the application to prepare the request, send it and parse the response.

You can store the webservice URL as a string "template" like so -

string wsUrlTemplate = "http://mywebservice.com/{0}/servicedosomething";

When you are about to issue a request, just format the string -

string wsUrl = string.Format(wsUrlTemplate, webServiceID);

If you have the SOAP XML request format, then store it as a template. When you need to issue the request, replace the placeholders with the actual values and send the request (with a POST option, if thats what the web services expect). A typical SOAP XML request template may look like -

string xmlRequestTemplate = "
<?xml version="1.0" encoding="utf-8" ?>
<Customer>
   <CustomerID>{0}</Customer>
</Customer>"

To prepare the request XML, you adopt the same approach as above - string format the xmlRequestTemplate and add the CustomerID. To issue the request, use HttpWebRequest to asynchronously issue the request and in the response handler, parse the XML response.

var request = HttpWebRequest.Create(wsUrl);
var result = (IAsyncResult)request.BeginGetResponse(ResponseCallback, request);

private void ResponseCallback(IAsyncResult result)
{
  var request = (HttpWebRequest)result.AsyncState;
  var response = request.EndGetResponse(result);

  using (var stream = response.GetResponseStream())
  using (var reader = new StreamReader(stream))
  {
    var contents = reader.ReadToEnd(); 
    // Parse the XML response
  }
}

Hope this gives you some ideas to proceed.

indyfromoz

indyfromoz
It did. I got it to work now. Thanks! Do you know how to put a header in the request btw? I'm trying it but I keep getting errors. I'm new to this, so I don't have any idea where to start looking. I get "The 'POST' header cannot be modified directly when I do this request.Headers["POST"] = headerString;
Carlo
Hi Carlo, I believe you can set the headers like so - request.Headers["Accept"] = "application/json"; I do not think you are allowed to modify POST/GET/etc in the request's header - indyfromoz
indyfromoz