【问题标题】:how to get Http Request from website by Windows App in C# [closed]如何在 C# 中通过 Windows 应用程序从网站获取 Http 请求 [关闭]
【发布时间】:2013-05-22 17:36:19
【问题描述】:

我为特定 URL 发送一个 Http 请求,并希望该 URL 通过响应向我发送反馈。我知道如何发送请求,但我不知道如何从该 URL 获得响应

谢谢!

【问题讨论】:

标签: c# windows-store-apps httprequest


【解决方案1】:

这是在 c# 中使用HTTPClient 类的示例代码。我的方案是使用async void 方法发送请求,如果发生任何错误,则在 Windows 商店应用程序中显示错误消息。

为了使用 HTTPClient 类,您必须使用 System.Net.Http 命名空间。为了显示简单的消息框,您必须使用Windows.UI.Popups 命名空间。 这是代码

using System.Net.Http; //this is for HTTPClient class
using Windows.UI.Popups //this is for Messagebox popup.

private async void getResponse()
        {
            try
            {
                HttpClient htClient = new HttpClient();
                string webUri = "www.google.com" //replace ur request web URI here
                string result = await htClient.GetStringAsync(webUri);
                //Form here you can code to extract the web response.
                //result is the web response string
            }
            catch (Exception c)
            {
                messageBox(c.Message);

            }
        }
//this is the method to show messagebox popup
protected async void messageBox(string msg)
        {
            var msgDlg = new Windows.UI.Popups.MessageDialog(msg);
            msgDlg.DefaultCommandIndex = 1;
            await msgDlg.ShowAsync();
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 2020-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多