简体   繁体   English

HTTPClient,C#和XML响应

[英]HTTPClient, C# and XML Response

I need to hit a REST service to obtain status information for some devices we have. 我需要点击REST服务以获取我们拥有的某些设备的状态信息。 This information is read-only. 此信息是只读的。

I have successfully tested what I need to test using Fiddler, but unsure how to accomplish it via HTTPClient or some other routine? 我已经使用Fiddler成功测试了我需要测试的内容,但是不确定如何通过HTTPClient或其他例程来完成它吗? I'd like to simply write a C# Console Application. 我只想编写一个C#控制台应用程序。

Basically, the task is this: 基本上,任务是这样的:

1) Logging into REST service via login URL using POST a) I must pass a User-Agent header matching a user-defined string b) I must pass Content-Type header matching text/xml c) I must send an XML payload of a user-defined XML doc. 1)使用POST通过登录URL登录REST服务a)我必须传递与用户定义的字符串匹配的User-Agent标头b)我必须传递与text / xml匹配的Content-Type标头c)我必须发送a的XML有效负载用户定义的XML文档。 d) There will be an XML response with a session ID and set-cookie value that I will need to obtained. d)我需要获得一个带有会话ID和set-cookie值的XML响应。

2) I must obtain a device list via device list URL using POST a) I must pass a User-Agent header matching a user-defined string b) I must pass Content-Type header matching text/xml c) I must pass Cookie header with the set-cookie value from initial response d) I must send an XML payload of a user-defined XML doc. 2)我必须使用POST通过设备列表URL获取设备列表a)我必须传递与用户定义的字符串匹配的User-Agent标头b)我必须传递与text / xml匹配的Content-Type标头c)我必须传递Cookie标头使用来自初始响应的set-cookie值d)我必须发送用户定义的XML文档的XML有效负载。 Will also need to include session ID from initial response. 还需要包括初始响应中的会话ID。 e) There will be an XML response with a list of devices... There may be more than one with a unique device ID. e)将有一个带有设备列表的XML响应...可能不止一个具有唯一设备ID的设备。 I will need to parse out a particular device IDs. 我将需要解析出特定的设备ID。

3) I must obtain a device status via the device status URL using POST a) I must pass a User-Agent header matching a user-defined string b) I must pass Content-Type header matching text/xml c) I must pass Cookie header with the set-cookie value from the initial response d) I must send and XML payload of a user-defined XML doc. 3)我必须使用POST通过设备状态URL获取设备状态a)我必须传递与用户定义的字符串匹配的User-Agent标头b)我必须传递与text / xml匹配的Content-Type标头c)我必须传递Cookie具有初始响应中的set-cookie值的标头d)我必须发送用户定义的XML文档的XML有效负载。 Will also need to include session ID from initial response. 还需要包括初始响应中的会话ID。 e) There will be an XML response with a list of device status information. e)将有一个带有设备状态信息列表的XML响应。 I will need to parse out particular status IDs. 我将需要解析出特定的状态ID。

If anyone can help me with this, it would be greatly appreciated. 如果有人可以帮助我,将不胜感激。

I'm also wondering if I should create a Login, Device list, Device Status object with all the XML results and populate this into an object? 我还想知道是否应该使用所有XML结果创建一个Login,Device列表,Device Status对象,并将其填充到一个对象中? Maybe this is overkill... 也许这太过分了...

The key point here is in using the same CookieContainer for all requests in order to preserve the cookies: 这里的关键点是对所有请求使用相同的CookieContainer以便保留cookie:

var cookieContainer = new CookieContainer();

1) Logging into REST service via login URL using POST a) I must pass a User-Agent header matching a user-defined string b) I must pass Content-Type header matching text/xml c) I must send an XML payload of a user-defined XML doc. 1)使用POST通过登录URL登录REST服务a)我必须传递与用户定义的字符串匹配的User-Agent标头b)我必须传递与text / xml匹配的Content-Type标头c)我必须发送a的XML有效负载用户定义的XML文档。 d) There will be an XML response with a session ID and set-cookie value that I will need to obtain. d)我需要获得一个带有会话ID和set-cookie值的XML响应。

var request = (HttpWebRequest)WebRequest.Create("http://example.com/login");
request.CookieContainer = cookieContainer;
request.UserAgent = "Some User Agent";
request.ContentType = "text/xml";
request.Method = "POST";
using (var stream = request.GetRequestStream())
using (var writer = new StreamWriter(stream))
{
    writer.Write("<foo>bar</foo>");
}

using (var response = request.GetResponse())
using (var stream = response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
    string body = reader.ReadToEnd();
    // the body will probably contain the SessionId you are looking for
    // go ahead, parse it and extract the necessary information
}

2) I must obtain a device list via device list URL using POST a) I must pass a User-Agent header matching a user-defined string b) I must pass Content-Type header matching text/xml c) I must pass Cookie header with the set-cookie value from initial response d) I must send an XML payload of a user-defined XML doc. 2)我必须使用POST通过设备列表URL获取设备列表a)我必须传递与用户定义的字符串匹配的User-Agent标头b)我必须传递与text / xml匹配的Content-Type标头c)我必须传递Cookie标头使用来自初始响应的set-cookie值d)我必须发送用户定义的XML文档的XML有效负载。 Will also need to include session ID from initial response. 还需要包括初始响应中的会话ID。 e) There will be an XML response with a list of devices... There may be more than one with a unique device ID. e)将有一个带有设备列表的XML响应...可能不止一个具有唯一设备ID的设备。 I will need to parse out a particular device IDs. 我将需要解析出特定的设备ID。

var request = (HttpWebRequest)WebRequest.Create("http://example.com/devices");
request.CookieContainer = cookieContainer;
request.UserAgent = "Some User Agent";
request.ContentType = "text/xml";
request.Method = "POST";
using (var stream = request.GetRequestStream())
using (var writer = new StreamWriter(stream))
{
    writer.Write("<foo><baz>bazinga</baz><sessionid>... the session id you have read from the body of the previous request might come here ...</sessionid></foo>");
}

using (var response = request.GetResponse())
using (var stream = response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
    string body = reader.ReadToEnd();
}

The important part is to reuse the same instance of the cookieContainer for both requests. 重要的部分是为两个请求重用cookieContainer的相同实例。 Bear in mind that this will only work of both requests are to the same domain which I suppose is your case because you appear to be scraping a website, not consuming a REST API. 请记住,这仅对两个请求都适用于您认为的同一个域有效,因为您似乎正在抓取网站,而不使用REST API。 A REST API would never rely on cookies by the way. REST API永远不会依赖Cookie。 But if for some reason yours does, and is cross domain then you will have to extract the cookies from the cookieContainer of the request and manually attach the value to the second request as the Cookie header: 但是,如果出于某种原因,并且您是跨域的,那么您将不得不从请求的cookieContainer中提取Cookie,并将值作为Cookie标头手动附加到第二个请求:

request.Headers[HttpRequestHeader.Cookie] = "Value of the cookie you have read from the cookieContainer of the first request";

3) I must obtain a device status via the device status URL using POST a) I must pass a User-Agent header matching a user-defined string b) I must pass Content-Type header matching text/xml c) I must pass Cookie header with the set-cookie value from the initial response d) I must send and XML payload of a user-defined XML doc. 3)我必须使用POST通过设备状态URL获取设备状态a)我必须传递与用户定义的字符串匹配的User-Agent标头b)我必须传递与text / xml匹配的Content-Type标头c)我必须传递Cookie具有初始响应中的set-cookie值的标头d)我必须发送用户定义的XML文档的XML有效负载。 Will also need to include session ID from initial response. 还需要包括初始响应中的会话ID。 e) There will be an XML response with a list of device status information. e)将有一个带有设备状态信息列表的XML响应。 I will need to parse out particular status IDs. 我将需要解析出特定的状态ID。

You should already have all the necessary bits to implement this yourself. 您应该已经拥有了所有必要的位来自己实现。

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

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