System.Net.WebException : The remote server returned an error: (415) UNSUPPORTED MEDIA TYPE
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."
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.
System.Net.WebException : The remote server returned an error: (415) UNSUPPORTED MEDIA TYPE的更多相关文章
- request 报错The remote server returned an error: (415) Unsupported Media Type.
开发时遇到个问题,程序访问数据库数据,给服务器发送请求时,老是报错,返回的错误页面是: HTTP Status 415 - Unsupported Media Type type Status rep ...
- unity 打包Error:WebException: The remote server returned an error: (403) Forbidden.
記一下在ios上打包出錯: UnityEditor.BuildPlayerWindow+BuildMethodException: 2 errors at UnityEditor.BuildPlaye ...
- Call Azure Queue get "The remote server returned an error: (400) Bad Request."
这几天开始研究Windows Azure, 在使用Azure Queue 的时候,CreateInfNotExists 总是抛出异常 "The remote server returned ...
- WebService:The remote server returned an error: (400) Bad Request
开发工具:VS2010.开发组件:WebService.运行环境:Windows 今天一个同事在进行计费接口联调试时,发现了一个非常奇怪的问题:接口在家里环境测试,一切正常,但是部署到现网环境之后,连 ...
- EWS code return Error : Request failed. The remote server returned an error: (403) Forbidden OR (401) Unauthorized
Following is my code. ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1 ...
- Darwin Streaming Server服务器mp4文件点播返回”415 Unsupported Media Type“错误
Darwin Streaming Server中mp4文件点播失败,通过抓包发现服务器返回”415 Unsupported Media Type“错误,如下: RTSP/ Unsupported Me ...
- HttpWebRequest WebExcepton: The remote server returned an error: (407) Proxy Authentication Required.
1. Supply the credentials of the Currently Logged on User to the Proxy object similar to this: // Be ...
- WP8 调用webservice 错误 The remote server returned an error: NotFound 解决
本人出错是由于本地的IIS不能被局域网其它机器访问导致的,如果你所用的本机IIS 也不可被其它机器访问,则可按照本文进行设置 具体操作时需要在防火墙设置中添加 入站规则 具体步骤如下: 1.控 ...
- CHECK_NRPE: Received 0 bytes from daemon. Check the remote server logs for error messages.
今天,在用icinga服务器端测试客户端脚本时,报如下错误: [root@mysql-server1 etc]# /usr/local/icinga/libexec/check_nrpe -H 192 ...
随机推荐
- 封装getElementsByClassName()
function getElementsByClassName(node,classname){ if(node.getElementsByClassName){ ...
- day5----正则
%a 本地(locale)简化星期名称 %A 本地完整星期名称 %b 本地简化月份名称 %B 本地完整月份名称 %c 本地相应的日期和时间 ...
- 百度ECHARTS 饼图使用心得 处理data属性
做过CRM系统的童鞋应该都或多或少接触过hicharts或者echarts等数据统计插件.用过这两款,个人感觉echarts的画面更好看.至于功能,只有适合自己的才是最好的. 今天来说说我使用echa ...
- SQL Server使用文件组备份降低备份文件占用的存储空间
对于DBA来说,备份和刷新简历是最重要的两项工作,如果发生故障后,发现备份也不可用,那么刷新简历的重要性就显现出来,哇咔咔!当然备份是DBA最重要的事情(没有之一),在有条件的情况下,我们应该在多个服 ...
- [OpenCV] 1、读取图片
>_<" 安装及配置请看http://www.cnblogs.com/zjutlitao/p/4042074.html >_<" 这篇是一个简单的在VS20 ...
- [游戏模版8] Win32 透明贴图
>_<:The same with previous introduction. In the InitInstance fanction make a little change: &g ...
- 深入理解javascript事件流
摘要:事件流这个东西是比较重要的,为了让自己更加理解js中的事件流,必须整理整理,梳理一下事件流的各种东西啊.本文大部分内容参考<javascript高级程序设计第三版> 先来一段书里的原 ...
- Atitit.api参数传递的设计
Atitit.api参数传递的设计 · 引言 · 形参和实参 · 命名实参 · 可选参数 · params,数目可变参数 · 方法解析与重载决策 · 参数传递 [重难点] · ref引用参数 ...
- 盘点mysql中容易被我们误会的地方
引语:mysql作为数据库的一大主力军,到处存在于我们各种系统中,相信大家都不陌生!但是,你知道你能用不代表你知道细节,那我们就来盘点盘点其中一些我们平时不太注意的地方,一来为了有趣,二来为了不让自己 ...
- UICollectionView基础
初始化部分: UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init]; self.myColl ...