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

标签 c# json http post 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":""
}

还有我的功能

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

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

"The remote server returned an error: (415) Unsupported Media Type."

任何人都知道;我哪里弄错了?

最佳答案

试试这个:

request.ContentType =  "application/json"

关于c# - 获取 (415) 不支持的媒体类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13140236/

相关文章:

php - 使用 json 发送邮件

java - 如何使用 Java 从相对 URL 构建绝对 URL?

c# - 类库项目上的 ExceptionLess

c# - 如何处理单击按钮时使用 Javascript 弹出的 Yes No 对话框

c# - 开拓者导航: Update URL without changing reloading page

JavaScript :Make convert array to json format

c# - Apple 的 Windows 版 Bonjour SDK

c# - Javascript 对象传递给 ASP.NET 处理程序

string - 在 HTTP 请求的上下文中,一个 255 字节的字符串有多长?

ruby - ruby 中的 HTTP 发布 NoMethodError