Posting data using webclient RRS feed

  • General discussion

  • Hi all

    I am trying to post data using the class WebClient from a silverlight application, but I cant seem to get access to the data posted on my webform. Request.Form always seems to be empty.

    Here is my code:

    private void btnSendData_Click(object sender, RoutedEventArgs e)


    string uri="http://localhost:53935/GetXmlResponse.ashx?Id1=5";

    WebClient client = new WebClient();

    client.Headers["Content-Type"] = "application/x-www-form-urlencoded";

    client.OpenWriteCompleted+=new OpenWriteCompletedEventHandler(client_OpenWriteCompleted);

    client.OpenWriteAsync(new Uri(uri), "POST", client);


    void client_OpenWriteCompleted(object sender, OpenWriteCompletedEventArgs e)


    string uri = "http://localhost:53935/GetXmlResponse.ashx?Id1=5";

    string postData = "id2=99&id3=56";

    byte[] b=ToByteArray(postData);

    e.Result.Write(b, 0, b.Length);

    WebClient client = e.UserState as WebClient;

    client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);

    client.DownloadStringAsync(new Uri(uri));


    void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)


    if (e.Error == null)


    lblResponse.Text = e.Result;




    lblResponse.Text = e.Error.Message;



    private byte[] ToByteArray(string data)


    UnicodeEncoding ue=new UnicodeEncoding();

    byte[] b=ue.GetBytes(data);

    return b;


    private string ToString(byte[] data)


    UnicodeEncoding ue = new UnicodeEncoding();

    string s = ue.GetString(data, 0, data.Length);

    return s;


    GetXmlResponse.ashx consists of the following:

    public void ProcessRequest(HttpContext context)


    context.Response.ContentType = "text/xml";

    XElement response = new XElement("reply",

    new XElement("id1", context.Request.QueryString["id1"]),

    new XElement("id2", context.Request.Form["id2"]));

    context.Response.Write(response); ;


    Any ideas on what I am doing wrong?

    • Changed type Marco Zhou Tuesday, October 28, 2008 8:15 AM off topic thread
    Thursday, October 23, 2008 10:39 AM