I would like to call a RESTfull service using the POST Method in my windows phone 8 app. So I need to insert the datas that I want to send in the body of the request after parsing it to JSON. To do so, I've used the following code:
enter cprivate void NextArrow_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
if (!String.IsNullOrEmpty(TxtBox_mail.Text))
{
Uri myUri = new Uri("http://myUri");
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(myUri);
myRequest.Method = "POST";
myRequest.ContentType = "application/json";
myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myRequest);
}
}
public void GetRequestStreamCallback(IAsyncResult callbackResult)
{
byte[] byteArray = null;
HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
// End the stream request operation
Stream postStream = myRequest.EndGetRequestStream(callbackResult);
// Create the post data
Dispatcher.BeginInvoke(() =>
{
string mailToCheck = TxtBox_mail.Text.ToString();
string postData = JsonConvert.SerializeObject(mailToCheck);
byteArray = Encoding.UTF8.GetBytes(postData);
});
// Add the post data to the web request
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
// Start the web request
myRequest.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), myRequest);
}
I've used the dispatcher to get the value of the textbox control on the UI Thread but the byteArray is always null. Somebody knows what could be wrong here ? Thanks in advance.