Global objects
	private HttpRequestMessage CloudConnectSendRequest = null;
	private Windows.Foundation.IAsyncOperationWithProgress<HttpResponseMessage, HttpProgress> CloudConnectSendRequestProgress = null;
Send Request



	//----- CREATE THE XML MESSAGE REQUEST -----
	String MessageToSend = "Hello";
	MessageToSend = MessageToSend.Replace("\t", "");        //Get rid of all the tabs

	HttpResponseMessage response = null;
	using (HttpClient client = CreateHttpClient())
	{
		//Create the request for the post
		CloudConnectSendRequest = new HttpRequestMessage(HttpMethod.Post, new Uri(DestinationUrl));

		byte[] bytes;
		bytes = System.Text.Encoding.ASCII.GetBytes(MessageToSend);

		//Create the HttpBufferContent from the bytes and set the request content
		IHttpContent content = new HttpBufferContent(bytes.AsBuffer());
		content.Headers.ContentType = HttpMediaTypeHeaderValue.Parse("text/xml; encoding='utf-8'");
		CloudConnectSendRequest.Content = content;

		//----- SEND THE MESSAGE ASYNCHRONOUSLY AND SET THE RESPONSE OBJECT-----
		CloudConnectSendRequestProgress = client.SendRequestAsync(CloudConnectSendRequest, HttpCompletionOption.ResponseContentRead);        //Read all content before flagging as complete
	}
Check for response
	if (CloudConnectSendRequestProgress.Status == Windows.Foundation.AsyncStatus.Completed)
	{
		HttpResponseMessage CloudConnectSendRequestResponse = CloudConnectSendRequestProgress.GetResults();
		if (CloudConnectSendRequestResponse.IsSuccessStatusCode)
		{
			if (CloudConnectSendRequestResponse.StatusCode == Windows.Web.Http.HttpStatusCode.Ok)
			{
				//------------------------------
				//----- CONNECTION SUCCESS -----
				//------------------------------
				//Stream responseStream = response.GetResponseStream();
				//string responseStr = new StreamReader(responseStream).ReadToEnd();
				String responseStr = CloudConnectSendRequestResponse.Content.ToString();

				System.Diagnostics.Debug.WriteLine("SERVER RESPONSE RECEIVED");
				System.Diagnostics.Debug.WriteLine(responseStr);


				//Convert inner XML to no longer be Html Encoded
				responseStr = WebUtility.HtmlDecode(responseStr);

			}
		}
	}



	if (CloudComms1msTimer == 0)
	{
		//----- TIMEOUT -----
		//Stop the request
		CloudConnectSendRequestProgress.Cancel();

		CloudCommsCurrentMode = CC_CONNECT_CLOUD_FAILED;
		break;
	}
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *