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 ...
随机推荐
- JAVA对象转化JSON出现死循环问题
主要是解决JSON因Hibernate映射生成的集合的转化出现的死循环问题. 这个方法很重要 public String ajaxJsonByObjectDirecdt(Object obj, Str ...
- 国内CDN公共库
CDN公共库是指将常用的JS库存放在CDN节点,以方便广大开发者直接调用.与将JS库存放在服务器单机上相比,CDN公共库更加稳定.高速.一般的CDN公共库都会包含全球所有最流行的开源JavaScrip ...
- Dynamic CRM 2013学习笔记(七)追踪、监控及性能优化
本文将介绍CRM的三个内容追踪.监控及性能优化.追踪是CRM里一个很有用的功能,它能为我们的CRM调试或解决错误.警告提供有价值的信息:我们可以用window的性能监控工具来了解CRM的性能状况:最后 ...
- 用groovy采集网页数据
首先,用 http://groovyconsole.appspot.com/ 测试下面的代码,发现引用总是失败. 下载了GGTS: https://spring.io/tools/ggts 测试成功: ...
- solr与.net系列课程(五)solrnet的使用
solr与.net系列课程(五)solrnet的使用 最近因项目比较忙,所以这篇文章出的比较晚,离上一篇文章已经有半个月的时间了,这节课我们来学下一下solr的.net客户端solrnet 出处 ...
- [WinAPI] API 6 [操作驱动器挂载点]
驱动器挂载点,又可以称作卷挂载点.挂载点实际上是操作系统或者用户设置的,用来进入一个逻辑驱动器或者卷的入口.在设置了卷的挂载点后,用户或者应用程序可以使用卷标或者指定的挂载点来进入卷.比如在“C:\” ...
- [ACM_动态规划] ZOJ 1425 Crossed Matchings(交叉最大匹配 动态规划)
Description There are two rows of positive integer numbers. We can draw one line segment between any ...
- jenkins 插件Copy Artifacts + Artifacts to copy
问题及遇到的错误: Unable to access upstream artifacts area /var/lib/jenkins/jobs/PROJECTNAME-Master-Branch/b ...
- JS获取元素CSS值的各种方法分析
先来看一个实例:如何获取一个没有设置大小的字体? <!DOCTYPE html> <html lang="en"> <head> <met ...
- fir.im Weekly - 如果让你重新做一款APP
设想下:如果让你重新做一款 APP ,你会用到哪些开发.设计等资源和工具? 本期的 Weekly 为大家分享了最近不错的 APP 开发资源,大部分是关于 iOS 开发. Android 开发.UI设计 ...