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,布布扣,bubuko.com

时间: 2024-08-05 03:34:37

System.Net.WebException : The remote server returned an error: (415) UNSUPPORTED MEDIA TYPE的相关文章

写Storage Table时报The remote server returned an error: (409) Conflict.

今天在往 Azure Storage Table中写入一条记录时,在刚开始老报 The remote server returned an error: (409) Conflict. 的异常,过了一阵子又好了.百思不得其解,后来终于在MSDN中找到了一段解释: When a table is successfully deleted, it is immediately marked for deletion and is no longer accessible to clients. Th

WebService:The remote server returned an error: (400) Bad Request

开发工具:VS2010.开发组件:WebService.运行环境:Windows 今天一个同事在进行计费接口联调试时,发现了一个非常奇怪的问题:接口在家里环境测试,一切正常,但是部署到现网环境之后,连续调用几次接口就会出现错误:The remote server returned an error: (400) Bad Request,然后回收一下WebService自己的程序池,再调用又可以了,再连续调用几次,又出现了上面的问题. 他来找我帮忙查问题,在了解整个过程之后,我就直接根据经验,设置

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: // Begin code change by jeff // Obtain the 'Proxy' of the Default browser. IWebProxy theProxy = aReq.Proxy; // Print the Proxy Url to the console. if (theP

The remote server returned an error: (401) Unauthorized - 最妖root case,mark下

昨天用.net console application读取sharepoint list列表,在本地一切正常,一传到服务器上就报401未授权的错误,尝试了各种办法都没有解决.最后发现原因是我的AD账号只能在本机使用,在其他电脑上都不能用,非常雷人的root cause.也许是被管AD账号的同事坑了,也许是我的人品昨天有点差,特mark下,一来作为提醒,二来也是说明做事不要随便,走正规流程就不会出现类似的囧境. 原文地址:https://www.cnblogs.com/61007257Steven

Remote Server returned '420 4.2.0 Recipient deferred because there is no Mdb'

通常NDR会给我们排错带来很明确的指示 今天聊一个我日常运维中遇到的问题,首先我的环境是"单域多站点"模式,共有3个Site的Exchange,收件流依次是Exchange Online Protection---A Site Cas---A Site MBX---对应收件地址数据库. 根据NDR,发现,邮件从EOP下来后,反复在A Site的MBX中跳跃,直道完全失败.经过ECP.ADSI的检查,发现是A Site没有正确同步其他两个Site DC的用户属性(homeMDB) 解决办

react native初学之路--The development server returned response error code 500错误

最近学习的时候经常遇见这种错误,出现的错误种类一般不是一种,在stackOverflow中找到一种解决办法: 在项目跟目录运行下列代码: 1.yarn remove babel-preset-react-native 2.yarn add [email protected] 如果没有安装yarn可以用npm

“psql: could not connect to server: Connection refused” Error when connecting to remote database

问题: I am trying to connect to a postgres database installed in a remote server using the following command: psql -h host_ip -U db_username -d db_name This the error that occurs: psql: could not connect to server: Connection refused Is the server runn

解决selemium报错 Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure

关于 Exception in thread "main" org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure. 代码中少了 webdriver.chrome.driver", "

selenium Remote Server 实现原理

selenium作为一个出色的web automation框架,被越来越多的企业采用究其原因,框架设计的比较remarkable, 作为一个开源的框架,能够开辟出一套协议,以至于针对app测试的appium采取相同的strategy.使用的是webdriver protocol的扩展版. 为什么说这个框架设计的比较好?究竟好在哪里? 先从表面上看: selenium automation framework 支持多语言,java.python.c#.JavaScript.Perl.ruby ..