【问题标题】:How to make an HTTP get request with parameters如何使用参数发出 HTTP get 请求
【发布时间】:2009-02-05 07:16:50
【问题描述】:

是否可以使用HTTP get 请求传递参数?如果是这样,我应该怎么做?我找到了HTTP 发布请求 (link)。在该示例中,字符串postData 被发送到网络服务器。我想用 get 来做同样的事情。谷歌在HTTPhere 上找到了这个例子。但是没有参数被发送到网络服务器。

【问题讨论】:

    标签: c# httpwebrequest get


    【解决方案1】:

    我的首选方式是这样。它为您处理转义和解析。

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        在 GET 请求中,您将参数作为查询字符串的一部分传递。

        string url = "http://somesite.com?var=12345";
        

        【讨论】:

        • 如果你在 iexplore 的地址栏中输入完整的 url,包括参数,我会得到与从 c# 获取 http 请求相同的响应
        • 已更新。无论如何,另一个答案更完整。
        【解决方案4】:

        WebRequest 对象对我来说似乎工作量太大。我更喜欢使用 WebClient 控件。

        要使用这个函数,你只需要创建两个 NameValueCollections 来保存你的参数和请求头。

        考虑以下函数:

            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】:

          您也可以直接通过 URL 传递值。

          如果你想调用方法 public static void calling(string name){....}

          那么你应该调用 usingHttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create("http://localhost:****/Report/calling?name=Priya); webrequest.Method = "GET"; webrequest.ContentType = "application/text";

          只要确保您在 URL 中使用 ?Object = value

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2020-11-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-11-27
            • 1970-01-01
            相关资源
            最近更新 更多