Oto kod używam:Jak wysłać JSON na serwer?
// create a request
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create(url); request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.Method = "POST";
// turn our request string into a byte stream
byte[] postBytes = Encoding.UTF8.GetBytes(json);
// this is important - make sure you specify type this way
request.ContentType = "application/json; charset=UTF-8";
request.Accept = "application/json";
request.ContentLength = postBytes.Length;
request.CookieContainer = Cookies;
request.UserAgent = currentUserAgent;
Stream requestStream = request.GetRequestStream();
// now send it
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();
// grab te response and print it out to the console along with the status code
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string result;
using (StreamReader rdr = new StreamReader(response.GetResponseStream()))
{
result = rdr.ReadToEnd();
}
return result;
Kiedy biegnę to, ja zawsze się 500 wewnętrzny błąd serwera.
Co robię źle?
Najpierw upewnij się, że dane, które publikujesz, są tym, czego oczekuje serwer. –
w rzeczywistości wygląda na to, że publikuję nieprawidłowe dane ... –
Dla ułatwienia pracy możesz dodać bibliotekę [json] (http://www.json.net) do swojego studia wizualnego. –