Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silverlight HTTP POST

Tags:

silverlight

I am just trying to perform an http post on http://www.test.com/test.asp?test1=3. Here is the code I have been trying to use:

    private void pif_test_conn()
    {


        Uri url = new Uri("http://www.test.com/test.asp?test1=3", UriKind.Absolute);



        if (httpResult == true)
        {


            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.ContentType = "application/x-www-form-urlencoded";
            request.Method = "POST";
           request.BeginGetResponse(new AsyncCallback(ReadCallback), request); 

        }



        return ;
    }


   private void ReadCallback(IAsyncResult asynchronousResult)
    {


        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;


        HttpWebResponse response =  (HttpWebResponse)request.EndGetResponse(asynchronousResult);

        using (StreamReader streamReader1 =  new StreamReader(response.GetResponseStream()))
        {

            string resultString = streamReader1.ReadToEnd();

             MessageBox.Show("Using HttpWebRequest: " + resultString, "Found", MessageBoxButton.OK);             
        }

    }

When I execute this code my program triggers the Application_UnhandledException event. Not sure what I am doing wrong.

like image 680
Weston Goodwin Avatar asked Apr 03 '26 01:04

Weston Goodwin


1 Answers

Are you trying to post to another host? That behavior could lead to XSS security problems, so that isnt available.


string responseValue = "";
AutoResetEvent syncRequest = new AutoResetEvent(false);
Uri address = new Uri(HtmlPage.Document.DocumentUri, "/sample.aspx");

WebRequest request = WebRequest.Create(address);
request.Method = "POST";
request.BeginGetRequestStream(getRequestResult =>
{
    // Send packet data
    using (Stream post = request.EndGetRequestStream(getRequestResult))
    {
        post.Write(buffer, 0, buffer.Length);
        post.Close();
    }

    // wait for server response
    request.BeginGetResponse(getResponseResult =>
    {
        WebResponse response = request.EndGetResponse(getResponseResult);
        responseValue=new StreamReader(response.GetResponseStream()).ReadToEnd();

        syncRequest.Set();

    }, null);

}, null);

syncRequest.WaitOne();

MessageBox.Show(
    "Using WebRequest: " + responseValue, 
    "Found", MessageBoxButton.OK);

HTH

like image 159
Rubens Farias Avatar answered Apr 08 '26 16:04

Rubens Farias



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!