2012-10-30 127 views
7

我所要做的就是,我在給定的URL 凡我JSON看起來像獲取(415)不支持的媒體類型錯誤

{ 
    "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":"[email protected]" 
    }, 
    "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":"" 
} 

而我的功能,發佈JSON數據

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."

任何人都有想法;我錯在哪裏?

+0

你有沒有得到這個解決?我被困在一個非常類似的問題[我在這裏發佈](http://stackoverflow.com/questions/14381385/system-net-webexception-when-sending-json-using-post-request-to-a- jira-api) – calebisstupid

回答

12

試試這個:

request.ContentType = "application/json" 
+0

試過但沒有運氣。感謝回覆 – user1785373

+0

如果這不起作用,這意味着服務器端不期待Json,所以問題出在服務器端而不是客戶端。 –

+0

對我來說這工作,似乎我不得不指定請求是一個JSON – break7533

0

我不是100%肯定,但我想你要發送文本爲ByteArray,試試這個:

req = (HttpWebRequest)HttpWebRequest.Create(uri); 
     req.CookieContainer = cookieContainer; 
     req.Method = "POST"; 
     req.ContentType = "text/json"; 
     byte[] byteArray2 = Encoding.ASCII.GetBytes(body); 
     req.ContentLength = byteArray2.Length; 
     Stream newStream = req.GetRequestStream(); 
     newStream.Write(byteArray2, 0, byteArray2.Length); 
     newStream.Close(); 
+0

嘗試它,但沒有運氣。感謝您的答覆 – user1785373

0

我改名爲我的項目,並更新了所有的在相同的命名空間之後,我得到了完全相同的消息。 我意識到,我還沒有更新在web.config(姓名和合同)的命名空間:

<system.serviceModel> 
    <services> 
    <service name="X.Y.Z.Authentication" behaviorConfiguration="ServiceBehaviour"> 
     <endpoint address="" binding="webHttpBinding" contract="X.Y.Z.IAuthentication" behaviorConfiguration="web" bindingConfiguration="defaultRestJsonp"></endpoint> 
    </service> 
    </...> 
</..> 

希望這有助於任何閱讀。

0

這是我的Web API函數創建一個代碼,接受JSON數據

string Serialized = JsonConvert.SerializeObject(jsonData); 
using (var client = new HttpClient()) 
{ 
    client.DefaultRequestHeaders.Accept.Clear(); 
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
    HttpContent content = new StringContent(Serialized, Encoding.Unicode, "application/json"); 
    var response = await client.PostAsync("http://localhost:1234", content); 
} 
1

如回答別人的問題是與ContentType的樣品。應該是'application/json'。

這是與舊的WebRequest

var parameters = Newtonsoft.Json.JsonConvert.SerializeObject(obj); 

var req = WebRequest.Create(uri); 

req.Method = "POST"; 
req.ContentType = "application/json"; 

byte[] bytes = Encoding.ASCII.GetBytes(parameters); 

req.ContentLength = bytes.Length; 

using (var os = req.GetRequestStream()) 
{ 
    os.Write(bytes, 0, bytes.Length); 

    os.Close(); 
} 

var stream = req.GetResponse().GetResponseStream(); 

if (stream != null) 
    using (stream) 
    using (var sr = new StreamReader(stream)) 
    { 
     return sr.ReadToEnd().Trim(); 
    } 
return "Response was null"; 
0

序列化要通過對其進行編碼的數據樣本。另外,提及 req.ContentType =「application/json」;

「馬丁」代碼的作品。

LoginInfo obj = new LoginInfo(); 

obj.username = uname; 
obj.password = pwd; 
var parameters = Newtonsoft.Json.JsonConvert.SerializeObject(obj); 

var req = WebRequest.Create(uri); 

req.Method = "POST"; 
req.ContentType = "application/json"; 

byte[] bytes = Encoding.ASCII.GetBytes(parameters); 

req.ContentLength = bytes.Length; 

using (var os = req.GetRequestStream()) 
{ 
    os.Write(bytes, 0, bytes.Length); 

    os.Close(); 
} 

var stream = req.GetResponse().GetResponseStream(); 

if (stream != null) 
    using (stream) 
    using (var sr = new StreamReader(stream)) 
    { 
     return sr.ReadToEnd().Trim(); 
    } 
0

有關的WebAPI >>如果你是calling this POST method from fiddler,只是在頭部添加此線以下。

Content-Type: application/json 
相關問題