Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since I was only supporting windows phone 7.5 and higher I was able to use this library (<a href="http://blogs.msdn.com/b/bclteam/archive/2012/11/22/10361314.aspx?PageIndex=3" rel="nofollow noreferrer">Microsoft.Bcl.Async</a>) to add async suppport to my plc and then use <a href="https://stackoverflow.com/questions/10565090/getting-the-response-of-a-asynchronous-httpwebrequest/10565229">this solution</a>.</p> <p>So my code ended up looking like this: </p> <pre><code> public async Task&lt;RequestResult&gt; RunRequestAsync(string requestUrl, string requestMethod, object body = null) { HttpWebRequest req = WebRequest.Create(requestUrl) as HttpWebRequest; req.ContentType = "application/json"; req.Credentials = new System.Net.NetworkCredential(User, Password); string auth = Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Format("{0}:{1}", User, Password))); var authHeader = string.Format("Basic {0}", auth); req.Headers["Authorization"] = authHeader; req.Method = requestMethod; //GET POST PUT DELETE req.Accept = "application/json, application/xml, text/json, text/x-json, text/javascript, text/xml"; if (body != null) { var json = JsonConvert.SerializeObject(body, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); byte[] formData = UTF8Encoding.UTF8.GetBytes(json); var requestStream = Task.Factory.FromAsync( req.BeginGetRequestStream, asyncResult =&gt; req.EndGetRequestStream(asyncResult), (object)null); var dataStream = await requestStream.ContinueWith(t =&gt; t.Result.WriteAsync(formData, 0, formData.Length)); Task.WaitAll(dataStream); } Task&lt;WebResponse&gt; task = Task.Factory.FromAsync( req.BeginGetResponse, asyncResult =&gt; req.EndGetResponse(asyncResult), (object)null); return await task.ContinueWith(t =&gt; { var httpWebResponse = t.Result as HttpWebResponse; return new RequestResult { Content = ReadStreamFromResponse(httpWebResponse), HttpStatusCode = httpWebResponse.StatusCode }; }); } private static string ReadStreamFromResponse(WebResponse response) { using (Stream responseStream = response.GetResponseStream()) using (StreamReader sr = new StreamReader(responseStream)) { //Need to return this response string strContent = sr.ReadToEnd(); return strContent; } } </code></pre> <p>And then I would call the code with something like:</p> <pre><code>public async Task&lt;bool&gt; SampleRequest() { var res = RunRequestAsync("https//whatever.com/update/1", "PUT"); return await res.ContinueWith(x =&gt; x.Result.HttpStatusCode == HttpStatusCode.OK); } </code></pre> <p>If that wasn't enough code for you feel free to check out the rest of the project <a href="https://github.com/eneifert/ZendeskApi_v2/blob/master/ZendeskApi_v2/Core.cs" rel="nofollow noreferrer">here</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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