银光 HTTP POST

标签 silverlight

我只是想在 http://www.test.com/test.asp?test1=3 上执行一个 http post .这是我一直在尝试使用的代码:

    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 事件。不确定我做错了什么。

最佳答案

您是否尝试发布到其他主机?该行为可能导致 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

关于银光 HTTP POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1528872/

相关文章:

silverlight - 在 Silverlight 中使用 Rx 进行 WCF 调用不适用于 TakeUntil

javascript - 将 SignalR 与自托管 WCF 结合使用并通过 HTML 客户端使用集线器代理

silverlight - 无法通过在 Silverlight 中提供相对路径来找到图像

c# - 无法解析 TargetName - Silverlight4 C#

javascript - 是否可以阻止 Silverlight 错误弹出错误对话框?

silverlight - 使用 MVVM 将数据传递到 Silverlight 4 中的子窗口

silverlight - 在 Silverlight 中绑定(bind)到 RelativeSource Self

mysql - Silverlight 和在线 MySQL 数据库

C# Silverlight - 构建时间轴?

Silverlight 4 WCF RIA 服务和 MVVM 没有那么简单