2012-10-30 13 views
7

Quello che devo fare è che ho per inviare i dati JSON in data URL Dove il mio JSON sembraGetting (415) Tipo di Supporto Non errore

{ 
    "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":"" 
} 

E la mia funzione

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

E quando lo sto postando; Ricevo un errore

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

Qualcuno ha idea a riguardo; dove mi sto sbagliando?

+0

Hai mai risolto questo problema? Sono bloccato da un problema molto simile [che ho postato qui] (http://stackoverflow.com/questions/14381385/system-net-webexception-when-sending-json-using-post-request-to-a- jira-api) – calebisstupid

risposta

12

Prova questo:

request.ContentType = "application/json" 
+0

Provato ma senza fortuna. Grazie per la risposta – user1785373

+0

Se questo non funziona, significa che il lato server non si aspetta Json, quindi il problema è sul lato server e non sul client. –

+0

per me questo ha funzionato, sembra che ho dovuto specificare che la richiesta era un JSON – break7533

0

Im non sicuro al 100%, ma credo che è necessario inviare il testo come un ByteArray, provate questo:

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

Provato ma senza fortuna.Grazie per la risposta – user1785373

0

ho rinominato il mio progetto e aggiornato tutti i namespace da mettere in relazione dopo di che ho ottenuto esattamente questo stesso messaggio. mi sono reso conto che non avevo aggiornato gli spazi dei nomi nel web.config (nome e contrattuali):

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

Spero che questo aiuti qualcuno leggendo questo.

0

questo è esempio di un codice che ho creato per funzione API web che accetta i dati 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

Come risposta da altri il problema è con il ContentType. Dovrebbe essere 'application/json'.

Ecco un esempio con il vecchio 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

serializzare i dati che si desidera passare e codificarlo. Inoltre, menzionare req.ContentType = "application/json";

codice "martin" funziona.

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

Per WebAPI >> Se siete calling this POST method from fiddler, basta aggiungere questo sotto la linea nell'intestazione.

Content-Type: application/json 
Problemi correlati