Wednesday, July 13, 2011

HttpWebRequest and HttpWebResponse in Windows Phone 7 (wp7) Silverlight

I thought to add information regarding with HttpWebRequest / HttpWebResponse in Windows Phone 7. Though WebRequest and response in Windows Phone 7 is little bit different but it is easy. 
Here is code :

public
HttpWebRequest objRequest = null;
public static string myRequestData = string.Empty;
public String urlAddress = “Your URL”;


public void doSend()
{
objRequest = (HttpWebRequest)WebRequest.Create(urlAddress.ToString());
objRequest.ContentType ="application/x-www-form-urlencoded";
objRequest.Method = "POST";
//Adding headers
objRequest.Headers["Header"]= "Your Value";
objRequest.Headers["Content-Language"] = "en-US";

myRequestData = “Your Request data”;

//Begins the asynchronous request
objRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback),objRequest);


}
private static void GetRequestStreamCallback(IAsyncResult asyncResult)
  {
            HttpWebRequest objHttpWebRequest = (HttpWebRequest)asyncResult.AsyncState;
            // End the operation
            Stream postStream = objHttpWebRequest.EndGetRequestStream(asyncResult);
            // Convert the string into a byte array.
            byte[] byteArray = Encoding.UTF8.GetBytes(myRequestData);
            // Write to the request stream.
            postStream.Write(byteArray, 0, myRequestData.Length);
            postStream.Close();

            // Start the asynchronous operation to get the response
         request.BeginGetResponse(new AsyncCallback(GetResponseCallback), objHttpWebRequest );
   }

private static void GetResponseCallback(IAsyncResult asyncResult)
        {
            HttpWebRequest objHttpWebRequest = (HttpWebRequest)asyncResult.AsyncState;
            HttpWebResponse objHttpWebResponse = HttpWebResponse)request.EndGetResponse(asyncResult);
            Stream objStreamResponse = objHttpWebResponse .GetResponseStream();
            StreamReader objStreamReader = new StreamReader(objStreamResponse );
            string responseString =  objStreamReader.ReadToEnd();            // Got response here
             MessageBox.Show("RESPONSE :" + responseString);
            // Close the stream object
            objStreamResponse .Close();
             objStreamReader.Close();
            objHttpWebResponse.Close();
   }



11 comments:

  1. Good job, but an urlencoding is missing. In both windows mobile and WIN CE OSs, this encoding does not exist and it should be implemented. But in windows 7, I'm not sure if the header "application/x-www-form-urlencoded" is sufficent.

    Cheers
    Hammadi Dali

    ReplyDelete
  2. thanks a lot:) Helped me immensely. the header "application/x-www-form-urlencoded" is sufficent in windows 7

    ReplyDelete
  3. request.BeginGetResponse(new AsyncCallback(GetResponseCallback), objHttpWebRequest );

    this line and

    HttpWebResponse objHttpWebResponse = HttpWebResponse)request.EndGetResponse(asyncResult);

    there is no request object...

    I changed that object with objHttpWebRequest but this time

    HttpWebResponse objHttpWebResponse = HttpWebResponse)objHttpWebRequest.EndGetResponse(asyncResult);

    this line give error
    An exception of type 'System.Net.WebException' occurred in System.Windows.ni.dll and wasn't handled before a managed/native boundary

    What can I do? I am trying to use this for my Windows Phone application.

    Please help me...

    kemalserkanyildirim@gmail.com

    ReplyDelete
    Replies
    1. Hi Kemal Serkan YILDIRIM,

      You must create the webrequest of URI which will send back the response for your request.

      Delete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. This comment has been removed by a blog administrator.

      Delete
    3. This comment has been removed by the author.

      Delete
  5. Hi, I tried above code and its working perfect but i want to know that how i can pass parameter to webservice. I am using php web service in WP7 and i read some blog and they says tht user webrequest.contentlength but i am unable to find it in WP7. If possible please share code line. Thanks...

    ReplyDelete
  6. Thank you so much.The article helped me to a greater extent.
    Good Job!!!!

    ReplyDelete