当前位置:  开发笔记 > 前端 > 正文

Silverlight HTTP POST

如何解决《SilverlightHTTPPOST》经验,为你挑选了1个好方法。

我只是想在http://www.test.com/test.asp?test1=3上发表一篇http帖子.这是我一直在尝试使用的代码:

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

    }

当我执行此代码时,我的程序会触发Application_UnhandledException事件.不确定我做错了什么.



1> Rubens Faria..:

你想张贴到另一个主人吗?这种行为可能导致XSS安全问题,因此不可用.

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

推荐阅读
和谐啄木鸟
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有