我正在尝试创建我的第一个 Windows 客户端(这是我第一次发布她的文章),它将与“Web 服务”进行通信,但是我在读取返回的响应标头时遇到了一些麻烦。在我的响应字符串中,我收到了一个不错的 JSON 文档(这是我的下一个问题),但我无法“查看/读取”响应中的标头,只能看到正文。
下面是我正在使用的代码。
WebClient MyClient = new WebClient();
MyClient.Headers.Add("Content-Type", "application/json");
MyClient.Headers.Add("User-Agent", "DIMS /0.1 +http://www.xxx.dk");
var urlstring = "http://api.xxx.com/users/" + Username.Text;
string response = MyClient.DownloadString(urlstring.ToString());
您可以像这样使用 WebClient.ResponseHeaders:
// Obtain the WebHeaderCollection instance containing the header name/value pair from the response.
WebHeaderCollection myWebHeaderCollection = myWebClient.ResponseHeaders;
Console.WriteLine("\nDisplaying the response headers\n");
// Loop through the ResponseHeaders and display the header name/value pairs.
for (int i=0; i < myWebHeaderCollection.Count; i++)
Console.WriteLine ("\t" + myWebHeaderCollection.GetKey(i) + " = " + myWebHeaderCollection.Get(i));
From https://msdn.microsoft.com/en-us/library/system.net.webclient.responseheaders(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.net.webclient.responseheaders(v=vs.110).aspx
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)