Note that there are some explanatory texts on larger screens.

plurals
  1. POWP7 - post json data to Web Service
    text
    copied!<p>I want to know the best way to send a small json string/ data to a web service. I can use WebClient or httpwebrequest that's not the issue, but i am mainly concerned about how to convert the string into json format and post with the request. </p> <p>As suggested , here i am using json.net , following is the code :</p> <pre><code> Uri url = new Uri("http://example.com"); //Create the web request object HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); webRequest.Method = "POST"; webRequest.ContentType = "application/json"; // Start the request webRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), webRequest); void GetRequestStreamCallback(IAsyncResult asynchronousResult) { HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState; // End the stream request operation Stream postStream = webRequest.EndGetRequestStream(asynchronousResult); // JObject json = new JObject( new JProperty("customer", new JObject ( new JProperty("phoneNumber", "07700555555"), new JProperty("name", "John") )) , new JProperty("pickupAddress", new JObject ( new JProperty("street", "1 Seagull Lane"), new JProperty("city", "London"), new JProperty("county", "London"), new JProperty("postcode", "E000XX"), new JProperty("country", "England"), new JProperty("longitude", "10.18"), new JProperty("latitude", "12.214") )) , new JProperty("destinationAddress", new JObject ( new JProperty("county", "London"), new JProperty("street", "1 Snow Lane"), new JProperty("longitude", "1.79"), new JProperty("latitude", "1.294"), new JProperty("postcode", "E00XX"), new JProperty("country", "England"), new JProperty("city", "London") )) , new JProperty("pickupTime", "1311467121460"), new JProperty("notes", "some notes"), new JProperty("accountNumber", "account1"), new JProperty("accountPassword", "account password") ); // // Create the post data // Demo POST data //byte[] byteArray = Encoding.UTF8.GetBytes(json.ToString()); //byte[] byteArray = Encoding.UTF8.GetBytes("1233456"); // JsonSerializer serializer = new JsonSerializer(); serializer.NullValueHandling = NullValueHandling.Ignore; using (StreamWriter sw = new StreamWriter(postStream)) using (JsonWriter writer = new JsonTextWriter(sw)) { //serializer.Serialize(writer, JsonConvert.SerializeObject(json)); json.WriteTo(writer,null); } // // Add the post data to the web request //postStream.Write(byteArray, 0, byteArray.Length); postStream.Close(); // Start the web request webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest); } void GetResponseCallback(IAsyncResult asynchronousResult) { try { HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState; HttpWebResponse response; // End the get response operation response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult); Stream streamResponse = response.GetResponseStream(); StreamReader streamReader = new StreamReader(streamResponse); var Response = streamReader.ReadToEnd(); MessageBox.Show(Response); streamResponse.Close(); streamReader.Close(); response.Close(); } catch (WebException e) { // Error treatment // ... } } </code></pre> <p>Now the code seems to be ok , json data is created ok , but response from server is showing : "the server response e = {"The remote server returned an error: NotFound."}"</p> <p>I don't know what's going wrong , please help friends ! Some one told me that i have to add something in header of the request i.e. The header should show: booking = {...JSON...}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload