简体   繁体   中英

Write HTTP request with some parameters and retrieve XML response

I'm writing an aspx page which has to send an http request to a known url with some parameters whose values are retrieved at runtime, then receive the response in xml form.
For example, the user presses the 'order' button so I have to send its order to http://foobar/xyz.do with parameter 'abc' being '123' and parameter 'def' being '456', then receive the response which could be something like < error code=332 > wtf?! < /error >'.
I don't even know where to start ;)

Cheers

You can use WebRequest class in the event handler of your order button. In other words, when the order button is pressed, you might run a code similar to this:

WebRequest request = WebRequest.Create("create your URL before this, and append parameters to it");
WebResponse response = request.GetResponse();
string responseBody = new StreamReader(response.GetResponseStream()).ReadToEnd();

Now, if the response is XML, you should use something like LINQ to XML or simply XmlDocument related classes to parse it. Otherwise, do appropriately.

If all the parameters are in the url and the response will always be XML, you could simply use the XmlDocument class and load the xml from the url. The framework in this case will do the heavy lifting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM