The 'Content-Type' header must be modified using the appropriate property or method. Parameter name: name

Anuj picture Anuj · Jul 13, 2017 · Viewed 16.8k times · Source

Hi I am using HttpWebRequest GET method to call a REST service. I am getting error :- ***'Content-Type' header must be modified using the appropriate property or method. Parameter name: name.***i checked all answer related this issue from stackoverflow.

My Code:-

    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);

}
}

Answer

coolcake picture coolcake · Feb 5, 2018

I too ended up with this problem. But realized the issue is with how you setting the content type.

The proper way to set is

request.ContentType = "application/json";