有没有办法在Windows Phone 8上的GET请求中发送Content-Type标头?

时间:2014-08-19 09:39:20

标签: windows-phone-8

我可以在Windows Phone 8 GET请求中发送Content-Type标头吗?

我必须使用客户提供的网络服务,要求消费者在Content-Type次请求时发送GET标头。有没有办法实现这个目标?

我尝试过使用System.Net.Http.HttpClientSystem.Net.HttpWebRequestSystem.Net.WebClient也使用了这一点)。

HttpClient抛出以下异常:

System.InvalidOperationException: Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.

WebClient抛出以下异常:

System.Net.WebException: An exception occurred during a WebClient request.  
---> System.Net.ProtocolViolationException: A request with this method cannot have a request body.

实际上,我完全同意例外情况,但我确实需要访问API。

1 个答案:

答案 0 :(得分:-2)

对于HttpWebRequest,您可以使用

webRequest = (HttpWebRequest)WebRequest.Create(<yourUri>);
webRequest.Method = "GET";
webRequest.ContentType = "application/json";

有关如何在HttpWebRequest中使用它的更多详细信息(示例使用POST),请参阅http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.contenttype(v=vs.110).aspx