简体   繁体   English

使用一些参数编写HTTP请求并检索XML响应

[英]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. 我正在编写一个aspx页面,该页面必须将带有一些参数的http请求发送到已知的url,这些参数的值在运行时被检索,然后以xml格式接收响应。
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?! 例如,用户按下“订单”按钮,因此我必须将其订单发送到http://foobar/xyz.do ,参数“ abc”为“ 123”,参数“ def”为“ 456”,然后接收响应可能类似于<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. 您可以在订单按钮的事件处理程序中使用WebRequest类。 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. 现在,如果响应是XML,则应该使用LINQ to XML类的东西LINQ to XML或者仅使用XmlDocument相关类来对其进行解析。 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. 如果所有参数都在url中,并且响应始终为XML,则可以简单地使用XmlDocument类并从url中加载xml。 The framework in this case will do the heavy lifting. 在这种情况下,框架将承担繁重的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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