如何使用参数创建HTTP get请求

时间:2022-08-22 17:46:03

Is it possible to pass parameters with an HTTP get request? If so, how should I then do it? I have found an HTTP post requst (link). In that example the string postData is sent to a webserver. I would like to do the same using get instead. Google found this example on HTTP get here. However no parameters are sent to the web server.

是否可以通过HTTP get请求传递参数?如果是这样,我该怎么做呢?我找到了一个HTTP post requst(链接)。在该示例中,字符串postData被发送到网络服务器。我想用get来做同样的事情。 Google在HTTP上找到了这个例子。但是,没有参数发送到Web服务器。

5 个解决方案

#1


In a GET request, you pass parameters as part of the query string.

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

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

#2


My preferred way is this. It handles the escaping and parsing for you.

我喜欢这样的方式。它为您处理转义和解析。

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

#3


First WebClient is easier to use; GET arguments are specified on the query-string - the only trick is to remember to escape any values:

第一个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);
        }

#4


The WebRequest object seems like too much work for me. I prefer to use the WebClient control.

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

To use this function you just need to create two NameValueCollections holding your parameters and request headers.

要使用此功能,您只需创建两个包含参数和请求标头的NameValueCollections。

Consider the following function:

考虑以下功能:

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

Add your querystring parameters (if required) as a NameValueCollection like so.

将查询字符串参数(如果需要)添加为NameValueCollection,如下所示。

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

Add your http headers (if required) as a NameValueCollection like so.

将您的http标头(如果需要)添加为NameValueCollection,如下所示。

        NameValueCollection RequestHttpHeaders = new NameValueCollection();
        RequestHttpHeaders.Add("Authorization", "Basic bGF3c2912XBANzg5ITppc2ltCzEF");

#5


You can also pass value directly via URL.

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

If you want to call method public static void calling(string name){....}

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

then you should call usingHttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create("http://localhost:****/Report/calling?name=Priya); webrequest.Method = "GET"; webrequest.ContentType = "application/text";

然后你应该调用usingHttpWebRequest webrequest =(HttpWebRequest)WebRequest.Create(“http:// localhost:**** / Report / calling?name = Priya); webrequest.Method =”GET“; webrequest.ContentType =”application /文本”;

Just make sure you are using ?Object = value in URL

只需确保使用?Object = value in URL

#1


In a GET request, you pass parameters as part of the query string.

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

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

#2


My preferred way is this. It handles the escaping and parsing for you.

我喜欢这样的方式。它为您处理转义和解析。

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

#3


First WebClient is easier to use; GET arguments are specified on the query-string - the only trick is to remember to escape any values:

第一个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);
        }

#4


The WebRequest object seems like too much work for me. I prefer to use the WebClient control.

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

To use this function you just need to create two NameValueCollections holding your parameters and request headers.

要使用此功能,您只需创建两个包含参数和请求标头的NameValueCollections。

Consider the following function:

考虑以下功能:

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

Add your querystring parameters (if required) as a NameValueCollection like so.

将查询字符串参数(如果需要)添加为NameValueCollection,如下所示。

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

Add your http headers (if required) as a NameValueCollection like so.

将您的http标头(如果需要)添加为NameValueCollection,如下所示。

        NameValueCollection RequestHttpHeaders = new NameValueCollection();
        RequestHttpHeaders.Add("Authorization", "Basic bGF3c2912XBANzg5ITppc2ltCzEF");

#5


You can also pass value directly via URL.

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

If you want to call method public static void calling(string name){....}

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

then you should call usingHttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create("http://localhost:****/Report/calling?name=Priya); webrequest.Method = "GET"; webrequest.ContentType = "application/text";

然后你应该调用usingHttpWebRequest webrequest =(HttpWebRequest)WebRequest.Create(“http:// localhost:**** / Report / calling?name = Priya); webrequest.Method =”GET“; webrequest.ContentType =”application /文本”;

Just make sure you are using ?Object = value in URL

只需确保使用?Object = value in URL