{"id":1132,"date":"2022-05-06T13:33:23","date_gmt":"2022-05-06T12:33:23","guid":{"rendered":"https:\/\/ibex.tech\/windows-iot\/?p=1132"},"modified":"2022-05-06T13:33:23","modified_gmt":"2022-05-06T12:33:23","slug":"async-http-get","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/uwp-programming-in-c\/tcp-ip\/http\/async-http-get","title":{"rendered":"Async HTTP Get"},"content":{"rendered":"\n<h5 class=\"wp-block-heading\">Global objects<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\tprivate HttpRequestMessage CloudConnectSendRequest = null;\n\tprivate Windows.Foundation.IAsyncOperationWithProgress&lt;HttpResponseMessage, HttpProgress&gt; CloudConnectSendRequestProgress = null;<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Send Request<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n\n\t\/\/----- CREATE THE XML MESSAGE REQUEST -----\n\tString MessageToSend = \"Hello\";\n\tMessageToSend = MessageToSend.Replace(\"\\t\", \"\");        \/\/Get rid of all the tabs\n\n\tHttpResponseMessage response = null;\n\tusing (HttpClient client = CreateHttpClient())\n\t{\n\t\t\/\/Create the request for the post\n\t\tCloudConnectSendRequest = new HttpRequestMessage(HttpMethod.Post, new Uri(DestinationUrl));\n\n\t\tbyte&#091;] bytes;\n\t\tbytes = System.Text.Encoding.ASCII.GetBytes(MessageToSend);\n\n\t\t\/\/Create the HttpBufferContent from the bytes and set the request content\n\t\tIHttpContent content = new HttpBufferContent(bytes.AsBuffer());\n\t\tcontent.Headers.ContentType = HttpMediaTypeHeaderValue.Parse(\"text\/xml; encoding='utf-8'\");\n\t\tCloudConnectSendRequest.Content = content;\n\n\t\t\/\/----- SEND THE MESSAGE ASYNCHRONOUSLY AND SET THE RESPONSE OBJECT-----\n\t\tCloudConnectSendRequestProgress = client.SendRequestAsync(CloudConnectSendRequest, HttpCompletionOption.ResponseContentRead);        \/\/Read all content before flagging as complete\n\t}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Check for response<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\tif (CloudConnectSendRequestProgress.Status == Windows.Foundation.AsyncStatus.Completed)\n\t{\n\t\tHttpResponseMessage CloudConnectSendRequestResponse = CloudConnectSendRequestProgress.GetResults();\n\t\tif (CloudConnectSendRequestResponse.IsSuccessStatusCode)\n\t\t{\n\t\t\tif (CloudConnectSendRequestResponse.StatusCode == Windows.Web.Http.HttpStatusCode.Ok)\n\t\t\t{\n\t\t\t\t\/\/------------------------------\n\t\t\t\t\/\/----- CONNECTION SUCCESS -----\n\t\t\t\t\/\/------------------------------\n\t\t\t\t\/\/Stream responseStream = response.GetResponseStream();\n\t\t\t\t\/\/string responseStr = new StreamReader(responseStream).ReadToEnd();\n\t\t\t\tString responseStr = CloudConnectSendRequestResponse.Content.ToString();\n\n\t\t\t\tSystem.Diagnostics.Debug.WriteLine(\"SERVER RESPONSE RECEIVED\");\n\t\t\t\tSystem.Diagnostics.Debug.WriteLine(responseStr);\n\n\n\t\t\t\t\/\/Convert inner XML to no longer be Html Encoded\n\t\t\t\tresponseStr = WebUtility.HtmlDecode(responseStr);\n\n\t\t\t}\n\t\t}\n\t}\n\n\n\n\tif (CloudComms1msTimer == 0)\n\t{\n\t\t\/\/----- TIMEOUT -----\n\t\t\/\/Stop the request\n\t\tCloudConnectSendRequestProgress.Cancel();\n\n\t\tCloudCommsCurrentMode = CC_CONNECT_CLOUD_FAILED;\n\t\tbreak;\n\t}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Global objects Send Request Check for response<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[118],"tags":[],"class_list":["post-1132","post","type-post","status-publish","format-standard","hentry","category-http"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/1132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/comments?post=1132"}],"version-history":[{"count":0,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/1132\/revisions"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=1132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=1132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=1132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}