C# System.Net.WebException:远程服务器返回错误:(415) UNSUPPORTED MEDIA TYPE

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16860904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 07:58:15  来源:igfitidea点击:

System.Net.WebException : The remote server returned an error: (415) UNSUPPORTED MEDIA TYPE

c#jsonrest

提问by petFoo

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."

我在访问一个安静的网络服务的一些代码时遇到了问题。运行此代码,它在 var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); 处出错。并且返回的异常是:“System.Net.WebException:远程服务器返回错误:(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.

我已经为 ContentType、MediaType 和 Accept 尝试了各种方法,但是服务开发人员给我的工作示例提供了 -H "Content-Type: application/json" 来卷曲,所以看起来 "application/ json" 是正确的值。他也做了 --data-binary,我认为 streamWriter 为我做了。

Does anyone know what might be causing this error?

有谁知道可能导致此错误的原因?

采纳答案by petFoo

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.

答案是将我创建带有身份验证令牌的标头的代码移动到我设置其他标头的代码上方。

回答by S Nash

One known reason for this error is mismatch of service name in service file and config file. That is Your service name and configuration service name don't match"

此错误的一个已知原因是服务文件和配置文件中的服务名称不匹配。那是您的服务名称和配置服务名称不匹配”

Right click on .svc file in Solution Explorer and select "View Markup" and paste correct sevice name it into the .config.

在解决方案资源管理器中右键单击 .svc 文件并选择“查看标记”并将正确的服务名称粘贴到 .config 中。