Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I use HttpWebRequest to GET from the web service, which returns me a JSON string. It looks something like this for a GET:</p> <pre><code>// Returns JSON string string GET(string url) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); try { WebResponse response = request.GetResponse(); using (Stream responseStream = response.GetResponseStream()) { StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.UTF8); return reader.ReadToEnd(); } } catch (WebException ex) { WebResponse errorResponse = ex.Response; using (Stream responseStream = errorResponse.GetResponseStream()) { StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.GetEncoding("utf-8")); String errorText = reader.ReadToEnd(); // log errorText } throw; } } </code></pre> <p>I then use <a href="http://json.codeplex.com/" rel="noreferrer">JSON.Net</a> to dynamically parse the string. Alternatively, you can generate the C# class statically from sample JSON output using this codeplex tool: <a href="http://jsonclassgenerator.codeplex.com/" rel="noreferrer">http://jsonclassgenerator.codeplex.com/</a></p> <p>POST looks like this:</p> <pre><code>// POST a JSON string void POST(string url, string jsonContent) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding(); Byte[] byteArray = encoding.GetBytes(jsonContent); request.ContentLength = byteArray.Length; request.ContentType = @"application/json"; using (Stream dataStream = request.GetRequestStream()) { dataStream.Write(byteArray, 0, byteArray.Length); } long length = 0; try { using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { length = response.ContentLength; } } catch (WebException ex) { // Log exception and throw as for GET example above } } </code></pre> <p>I use code like this in automated tests of our web service.</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