System.Net.WebException : The remote server returned an error: (415) UNSUPPORTED MEDIA TYPE
2014-08-08 18:42
633 查看
I am having problems with a bit of code that accesses a restful web service. Running this code, it errors out at var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); and the exception returned is: "System.Net.WebException : The remote server returned an error: (415) UNSUPPORTED MEDIA TYPE."
I have tried various things for the ContentType, MediaType, and Accept, but the working example given to me by the developer of the service supplies -H "Content-Type: application/json" to curl, so it would seem that "application/json" is the correct value. He also does --data-binary, which I assume streamWriter does for me.
Does anyone know what might be causing this error?
Figured it out.
When I do:
I accidentally blow away all of the existing headers that were created by doing:
The answer is to move the code where I create the header with the auth token above the code where I set the other headers.
来源
public bool CreateAccount(string myUsername, string url, string authtoken) { try { HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); httpWebRequest.ContentType = "application/json"; httpWebRequest.MediaType="application/json"; httpWebRequest.Accept="application/json"; httpWebRequest.Method = "POST"; WebHeaderCollection headers = new WebHeaderCollection(); headers.Add("Authorization: Token"+authtoken); httpWebRequest.Headers = headers; using (StreamWriter streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { streamWriter.Write("{username : '"+myUsername+"'}"); } var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); // Fails on this line. using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { JavaScriptSerializer jsSerializer = new JavaScriptSerializer(); string json = streamReader.ReadToEnd(); } return true; } catch (WebException e) { throw e; return false; } //return true; }
I have tried various things for the ContentType, MediaType, and Accept, but the working example given to me by the developer of the service supplies -H "Content-Type: application/json" to curl, so it would seem that "application/json" is the correct value. He also does --data-binary, which I assume streamWriter does for me.
Does anyone know what might be causing this error?
Figured it out.
When I do:
WebHeaderCollection headers = new WebHeaderCollection(); headers.Add("Authorization: Token "+authtoken); httpWebRequest.Headers = headers;
I accidentally blow away all of the existing headers that were created by doing:
httpWebRequest.ContentType = "application/json"; httpWebRequest.MediaType="application/json"; httpWebRequest.Accept="application/json"; httpWebRequest.Method = "POST";
The answer is to move the code where I create the header with the auth token above the code where I set the other headers.
来源
相关文章推荐
- request 报错The remote server returned an error: (415) Unsupported Media Type.
- request 报错The remote server returned an error: (415) Unsupported Media Type.
- The remote server returned an error: (415) Cannot process the message because the content type 'text
- Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 500 解决办法
- HttpWebRequest WebExcepton: The remote server returned an error: (407) Proxy Authentication Required.
- Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 500
- Silverlight调用WebService出现CommunicationException The remote server returned an error: NotFound的一种可能原因
- The remote server returned an error (415) Cannot process the message
- Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 500 解决办法
- 关于Silverlight中WebService调用出现CommunicationException The remote server returned an error: NotFound
- The remote server returned an error: (400) Bad Request. --- RosettaNet 3.3
- The remote server returned an error: (403) Forbidden.
- Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The s
- Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The s
- Silverlight调用WCF服务出现The remote server returned an error: NotFound可能原因
- Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The s
- Error creating control: The type initializer for 'Microsoft.Web.Design.ExtenderInformationServiceProvider' threw an exception
- Silverlight + WCF错误“The remote server returned an error: NotFound.”的解决方法
- Error: Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server
- The remote server returned an error: (404) Not Found