I am reposting this second question from my original post (Http Post for Windows Phone 8) because my primary question was alreayd answered.
This is my updated code with the help of @Hunter McMillen.. I am now trying to get a responseCallback from the server. The problem is the GetResponseCallback => (HttpWebResponse)httpWebRequest.EndGetResponse(GetResponseCallback)
line in the second using statement, it is displaying
An exception of type 'System.Net.WebException' occurred in System.Windows.ni.dll but was not handled in user code
If there is a handler for this exception, the program may be safely continued.
This error occured before when I was using the first example. Does anyone know how to solve this?
private static async void HttpPostData(){
string url = "http://www.mytunnel.com/api/purchases";
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "text/plain";
//httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.AllowWriteStreamBuffering = true;
httpWebRequest.Method = "POST";
//httpWebRequest.ContentLength = jsonAsBytes.Length;
try{
using (var stream = await Task.Factory.FromAsync<Stream>(httpWebRequest.BeginGetRequestStream, httpWebRequest.EndGetRequestStream, null))
{
byte[] jsonAsBytes = Encoding.UTF8.GetBytes("{ \"data\" : \"json\" }");
await stream.WriteAsync(jsonAsBytes, 0, jsonAsBytes.Length);
}
}
catch (Exception e) { Debug.WriteLine(e.Message); }
httpWebRequest.BeginGetResponse(new AsyncCallback(ReadWebRequestCallback), httpWebRequest);
}
private static void ReadWebRequestCallback(IAsyncResult callbackResult)
{
HttpWebRequest myRequest = callbackResult.AsyncState as HttpWebRequest;
try
{
HttpWebResponse response = myRequest.EndGetResponse(callbackResult) as HttpWebResponse;
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
String s = sr.ReadToEnd();
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => { MessageBox.Show(s); });
}
}
catch (WebException webExcp)
{
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => { MessageBox.Show(webExcp.ToString()); });
}
}