必须使用适当的属性或方法修改“Content-Type”标头。参数名称:名称

时间:2017-07-13 09:17:20

标签: c# wcf-data-services

您好我正在使用HttpWebRequest GET方法来调用REST服务。我收到错误: - ***'Content-Type'标头必须使用适当的属性或方法进行修改。参数名称:name。***我从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);

}
}

1 个答案:

答案 0 :(得分:9)

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

正确的设置方法是

request.ContentType = "application/json";