如何使用参数进行HTTP GET请求

77

是否可以在 HTTP 的 get 请求中传递参数?如果可以,应该如何实现?我已经找到了一个关于 HTTP post 请求的例子 (链接)。在那个例子中,字符串 postData 被发送到了 web 服务器。我想使用 get 来实现相同的功能。在谷歌上找到了这个关于 HTTP get 请求的例子(链接)。但是没有参数被发送给 web 服务器。

6个回答

154

我偏爱这种方式。它会为你处理转义和解析。

WebClient webClient = new WebClient();
webClient.QueryString.Add("param1", "value1");
webClient.QueryString.Add("param2", "value2");
string result = webClient.DownloadString("http://theurl.com");

99

首先,WebClient 更易于使用;GET 参数是在查询字符串上指定的——唯一需要记住的技巧是要转义任何值:

        string address = string.Format(
            "http://foobar/somepage?arg1={0}&arg2={1}",
            Uri.EscapeDataString("escape me"),
            Uri.EscapeDataString("& me !!"));
        string text;
        using (WebClient client = new WebClient())
        {
            text = client.DownloadString(address);
        }

49
在GET请求中,您将参数作为查询字符串的一部分传递。
string url = "http://somesite.com?var=12345";

如果您在iexplore的地址栏中输入完整的URL,包括参数,那么我是否会得到与从C#发出HTTP请求相同的响应? - CruelIO
1
已更新。另一个答案更完整。 - EndangeredMassa

8

WebRequest对象对我来说似乎太麻烦了。我更喜欢使用WebClient控件。

要使用此功能,您只需要创建两个NameValueCollection来保存您的参数和请求头。

请考虑以下函数:

    private static string DoGET(string URL,NameValueCollection QueryStringParameters = null, NameValueCollection RequestHeaders = null)
    {
        string ResponseText = null;
        using (WebClient client = new WebClient())
        {
            try
            {
                if (RequestHeaders != null)
                {
                    if (RequestHeaders.Count > 0)
                    {
                        foreach (string header in RequestHeaders.AllKeys)
                            client.Headers.Add(header, RequestHeaders[header]);
                    }
                }
                if (QueryStringParameters != null)
                {
                    if (QueryStringParameters.Count > 0)
                    {
                        foreach (string parm in QueryStringParameters.AllKeys)
                            client.QueryString.Add(parm, QueryStringParameters[parm]);
                    }
                }
                byte[] ResponseBytes = client.DownloadData(URL);
                ResponseText = Encoding.UTF8.GetString(ResponseBytes);
            }
            catch (WebException exception)
            {
                if (exception.Response != null)
                {
                    var responseStream = exception.Response.GetResponseStream();

                    if (responseStream != null)
                    {
                        using (var reader = new StreamReader(responseStream))
                        {
                            Response.Write(reader.ReadToEnd());
                        }
                    }
                }
            }
        }
        return ResponseText;
    }

如果需要,请将您的查询字符串参数作为NameValueCollection添加,如下所示。

        NameValueCollection QueryStringParameters = new NameValueCollection();
        QueryStringParameters.Add("id", "123");
        QueryStringParameters.Add("category", "A");

将您的HTTP标头(如果需要)作为NameValueCollection添加,如下所示。
        NameValueCollection RequestHttpHeaders = new NameValueCollection();
        RequestHttpHeaders.Add("Authorization", "Basic bGF3c2912XBANzg5ITppc2ltCzEF");

5

使用多个参数的GET请求:

curl --request GET --url http://localhost:8080/todos/?limit=10&offset=2 --header 'content-type:application/json'


2
您也可以通过URL直接传递值。
如果您想调用方法public static void calling(string name){....}
那么您应该使用以下方式调用:HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create("http://localhost:****/Report/calling?name=Priya); webrequest.Method = "GET"; webrequest.ContentType = "application/text"; 只需确保在URL中使用?Object = value即可。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接