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 :

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);

            // 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();


  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.

    Hammadi Dali

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

  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 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...

    1. Hi Kemal Serkan YILDIRIM,

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

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

    1. This comment has been removed by the author.

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

    3. This comment has been removed by the author.

  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...

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