SilverLight 2.0 的 C# Web 请求

发布于 2024-07-14 19:12:02 字数 1233 浏览 10 评论 0原文

我一直在使用以下代码从 SilverLight 中的 Apache 2.2 获取简单的 Web 响应,但无济于事。

    private void bDoIt_Click(object sender, RoutedEventArgs e)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("/silverlight/TestPage2.html"));

        request.Method = "POST";
        request.ContentType = "text/xml";

        request.BeginGetRequestStream(new AsyncCallback(RequestProceed), request);
    }

    private void RequestProceed(IAsyncResult asuncResult)
    {
        HttpWebRequest request = (HttpWebRequest)asuncResult.AsyncState;

        StreamWriter postDataWriter = new StreamWriter(request.EndGetRequestStream(asuncResult));
        postDataWriter.Close();

        request.BeginGetResponse(new AsyncCallback(ResponceProceed), request);

    }

    private void ResponceProceed(IAsyncResult asuncResult)
    {
        HttpWebRequest request = (HttpWebRequest)asuncResult.AsyncState;

        HttpWebResponse responce = (HttpWebResponse)request.EndGetResponse(asuncResult);
        StreamReader responceReader = new StreamReader(responce.GetResponseStream());

        string responceString = responceReader.ReadToEnd();

        txtData.Text = responceString;
    }

有人没有更好的方法吗?

I have been using the following code to obtain a simple web response from Apache 2.2 in SilverLight to no avail.

    private void bDoIt_Click(object sender, RoutedEventArgs e)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("/silverlight/TestPage2.html"));

        request.Method = "POST";
        request.ContentType = "text/xml";

        request.BeginGetRequestStream(new AsyncCallback(RequestProceed), request);
    }

    private void RequestProceed(IAsyncResult asuncResult)
    {
        HttpWebRequest request = (HttpWebRequest)asuncResult.AsyncState;

        StreamWriter postDataWriter = new StreamWriter(request.EndGetRequestStream(asuncResult));
        postDataWriter.Close();

        request.BeginGetResponse(new AsyncCallback(ResponceProceed), request);

    }

    private void ResponceProceed(IAsyncResult asuncResult)
    {
        HttpWebRequest request = (HttpWebRequest)asuncResult.AsyncState;

        HttpWebResponse responce = (HttpWebResponse)request.EndGetResponse(asuncResult);
        StreamReader responceReader = new StreamReader(responce.GetResponseStream());

        string responceString = responceReader.ReadToEnd();

        txtData.Text = responceString;
    }

Does anyone no a better method of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何以畏孤独 2024-07-21 19:12:02

您是否尝试过 WebClient? 这存在于 silverlight 上,可能会让生活变得更轻松。 大概您需要 UploadStringAsync

另外 - 我相信你需要使用绝对网址; 如果您不想硬编码(相当合理),您可以从以下位置获取主机:

string url = App.Current.Host.Source.AbsoluteUri;

然后使用 string / etc 方法来制作正确的“http://yoursite/whatever/your.page";

请注意,silverlight 仅允许 (IIRC) 连接到主机站点。

Have you tried WebClient? This exists on silverlight, and might make life easier. Presumably you'd want UploadStringAsync.

Also - I believe you need to use and absolute url; if you don't want to hard code (quite reasonably), you can get your host from:

string url = App.Current.Host.Source.AbsoluteUri;

Then use string / etc methods to make the correct "http://yoursite/whatever/your.page";

Note that silverlight only allows (IIRC) connections to the host site.

流心雨 2024-07-21 19:12:02

您可以将 BeginGetResponse 调用作为示例测试用例中的第一个调用,仅当您打算将一些 POST 数据传递到请求的页面时才需要 BeginGetRequestStream 调用。

You can do the BeginGetResponse call as the first call in your sample test case, the BeginGetRequestStream call is only needed if you are intending to pass some POST data to the requested page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文