我可以在Windows Phone 8 GET请求中发送Content-Type标头吗?
我必须使用客户提供的网络服务,要求消费者在Content-Type
次请求时发送GET
标头。有没有办法实现这个目标?
我尝试过使用System.Net.Http.HttpClient
和System.Net.HttpWebRequest
(System.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。
答案 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。