获取(415)不支持的媒体类型错误

use*_*373 15 c# post json http httpwebrequest

我要做的是我必须在给定的URL中发布JSON数据我的JSON看起来像

{
    "trip_title":"My Hotel Booking",
    "traveler_info":{
        "first_name":"Edward",
        "middle_name":"",
        "last_name":"Cullen",
        "phone":{
            "country_code":"1",
            "area_code":"425",
            "number":"6795089"
        },
        "email":"asdv@gmail.com"
    },
    "billing_info":{
        "credit_card":{
            "card_number":"47135821",
            "card_type":"Visa",
            "card_security_code":"123",
            "expiration_month":"09",
            "expiration_year":"2017"
        },
        "first_name":"Edward",
        "last_name":"Cullen",
        "billing_address":{
            "street1":"Expedia Inc",
            "street2":"108th Ave NE",
            "suite":"333",
            "city":"Bellevue",
            "state":"WA",
            "country":"USA",
            "zipcode":"98004"
        },
        "phone":{
            "country_code":"1",
            "area_code":"425",
            "number":"782"
        }
    },
    "marketing_code":""
}
Run Code Online (Sandbox Code Playgroud)

而我的功能

string message = "URL";
_body="JSON DATA";
HttpWebRequest request = HttpWebRequest.Create(message) as HttpWebRequest;
if (!string.IsNullOrEmpty(_body))
{
    request.ContentType =  "text/json";
    request.Method =  "POST";

    using (var streamWriter = new StreamWriter(request.GetRequestStream()))
    {
        streamWriter.Write(_body);
        streamWriter.Flush();
        streamWriter.Close();
    }
}

using (HttpWebResponse webresponse = request.GetResponse() as HttpWebResponse)
{
    using (StreamReader reader = new StreamReader(webresponse.GetResponseStream()))
    {
        string response = reader.ReadToEnd();
    }
}
Run Code Online (Sandbox Code Playgroud)

当我发布它时; 我收到了一个错误

"远程服务器返回错误:(415)不支持的媒体类型."

任何人都有这个想法; 在哪里,我在误解?

nie*_*eve 20

试试这个:

request.ContentType =  "application/json"
Run Code Online (Sandbox Code Playgroud)


Rak*_*ure 6

对于WebAPI >>如果是calling this POST method from fiddler,只需将其添加到标题下面的行中。

Content-Type: application/json
Run Code Online (Sandbox Code Playgroud)