【问题标题】:The 'Content-Type' header must be modified using the appropriate property or method. Parameter name: name必须使用适当的属性或方法修改“Content-Type”标头。参数名称:名称
【发布时间】:2017-12-17 23:38:10
【问题描述】:

您好,我正在使用 HttpWebRequest GET 方法调用 REST 服务。我收到错误消息:- ***'Content-Type' 标头必须使用适当的属性或方法进行修改。参数名称:名称。***我从stackoverflow检查了所有与此问题相关的答案。

我的代码:-

    using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        Getvalue(TextBox1.Text,TextBox2.Text,TextBox3.Text);
    }

    private void Getvalue(string text1, string text2, string text3)
    {

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "POST";
        request.KeepAlive = true;
        request.ContentType = "appication/json";
        request.Headers.Add("Content-Type", "appication/json");
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        string myResponse = "";
 using (System.IO.StreamReader sr = new system.IO.StreamReader(response.GetResponseStream()))
        {
            myResponse = sr.ReadToEnd();
        }
        Response.Write(myResponse);

}
}

【问题讨论】:

  • 那么你为什么要设置ContentType 属性然后试图在标题中设置它?只需删除 request.Headers.Add 行 - 设置属性应该是您所需要的。请注意,如果您提供真实代码,它会更有用 - 您目前提供的代码不会编译。
  • 你还需要 application/json,而不是 appication/json。
  • 对不起..!不使用 GET 方法调用 REST 服务。使用POST方法调用rest服务
  • 这有什么区别?
  • 好的,乔恩我改变了你提到的代码,现在我得到了其他错误:远程服务器返回一个错误:(400)错误请求。

标签: c# wcf-data-services


【解决方案1】:

我也遇到了这个问题。但意识到问题在于您如何设置内容类型。

正确的设置方式是

request.ContentType = "application/json";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    • 2012-02-28
    相关资源
    最近更新 更多