开发者

Dynamic web service request

开发者 https://www.devze.com 2023-01-21 15:59 出处:网络
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.:

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, migh开发者_StackOverflow社区t be the WP7 framework or something.

Thanks!


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

0

精彩评论

暂无评论...
验证码 换一张
取 消