WP7: Sending "POST" data and receive response

Let's send "POST" data from our Windows Phone and get response.
  1. Create Windows Phone Application named "SendPOSTdata".
  2. In MainPage.xaml.cs include some "using":
    using System;
    using System.IO;
    using System.Text;
    using System.Diagnostics;
    using System.Net;
    using System.Windows;
    using Microsoft.Phone.Controls;
    
  3. Create loaded event handler:
    using System;
    public MainPage()
    {
        InitializeComponent();
    
        Loaded += new RoutedEventHandler(MainPage_Loaded);
    }
    
    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        System.Uri myUri = new System.Uri("http://myPageUrlAddress.com/");
        HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(myUri);
        myRequest.Method = "POST";
        myRequest.ContentType = "application/x-www-form-urlencoded";
        myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback),myRequest);
    }
    
  4. Now we need create our "POST" data stream:
    void GetRequestStreamCallback(IAsyncResult callbackResult)
    {
        HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
        // End the stream request operation
        Stream postStream = myRequest.EndGetRequestStream(callbackResult);
    
        // Create the post data
        string postData = "param1=value1&param2=value2";
        byte[] 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);
    }
    
  5. And at the end receive response:
    void GetResponsetStreamCallback(IAsyncResult callbackResult)
    {
        HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
        using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream()))
        {
            string result = httpWebStreamReader.ReadToEnd();
            //For debug: show results
            Debug.WriteLine(result);
        }
    }
    
  6. Run.

11 comments:

  1. if I want to send image from image1, how I can put this in code

    string postData = "param1=value1&param2=value2";
    byte[] byteArray = Encoding.UTF8.GetBytes(postData);
    ?

    ReplyDelete
  2. Can you explain ,if i want to use database which is made in my local pc which is in sql server 2008 ,then what is the way to store in it and retrive data form it.please help me
    Thanks in advance.

    ReplyDelete
  3. how to upload image using HttpWebRequest to server?

    ReplyDelete
  4. hey, i have this web page that has a form that can take in a user name and a password, i want my wp app to take these entries from user, and then submit the form, the background web page must not be visible to the user, is there a way to do so?

    ReplyDelete
  5. Am getting error
    + $exception {System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound.
    at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
    at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClasse.b__d(Object sendState)
    at System.Net.Browser.AsyncHelper.<>c__DisplayClass1.b__0(Object sendState)
    --- End of inner exception stack trace ---
    at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
    at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
    at Line_App.MainPage.GetResponsetStreamCallback(IAsyncResult callbackResult)
    at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClass1d.b__1b(Object state2)} System.Exception {System.Net.WebException}

    ReplyDelete
    Replies
    1. That particular error can mean anything, it is just the generic error that WCF uses to say "something went wrong". Read about it
      http://social.msdn.microsoft.com/Forums/en-US/18898e77-6b61-4b67-9187-f754c7bfaabc/systemnetwebexception-the-remote-server-returned-an-error-notfound?forum=synclab
      http://stackoverflow.com/questions/2920916/the-remote-server-returned-an-error-notfound
      http://stackoverflow.com/questions/21176574/system-net-webexception-the-remote-server-returned-an-error-notfound-with-post

      Delete
  6. System.Net.WebException was unhandled by user code
    HResult=-2146233079
    Message=The remote server returned an error: NotFound.
    Source=System.Windows
    This Error i got Please Correct me .....

    ReplyDelete
  7. Thank you! This helped me today.

    ReplyDelete
  8. {System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound.
    at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
    at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClasse.

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete