How to make an HTTP get request with parameters
Asked Answered
E

6

83

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.

Eddie answered 5/2, 2009 at 7:16 Comment(0)
C
54

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

string url = "http://somesite.com?var=12345";
Corundum answered 5/2, 2009 at 7:20 Comment(2)
If you enter the complete url including the parameters in the adresse bar of iexplore, do I then get the same response as is make a http request get from c#Eddie
Updated. The other answer is more complete anyway.Corundum
P
156

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");
Psaltery answered 20/6, 2013 at 16:59 Comment(0)
G
99

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

        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);
        }
Ghost answered 5/2, 2009 at 7:27 Comment(0)
C
54

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

string url = "http://somesite.com?var=12345";
Corundum answered 5/2, 2009 at 7:20 Comment(2)
If you enter the complete url including the parameters in the adresse bar of iexplore, do I then get the same response as is make a http request get from c#Eddie
Updated. The other answer is more complete anyway.Corundum
C
8

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

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

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 QueryStringParameters = new NameValueCollection();
        QueryStringParameters.Add("id", "123");
        QueryStringParameters.Add("category", "A");

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

        NameValueCollection RequestHttpHeaders = new NameValueCollection();
        RequestHttpHeaders.Add("Authorization", "Basic bGF3c2912XBANzg5ITppc2ltCzEF");
Chessman answered 9/7, 2014 at 15:51 Comment(0)
L
5

GET request with multiple params:

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

Lipkin answered 15/4, 2022 at 15:16 Comment(0)
J
2

You can also pass value directly via URL.

If you want to call method 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";

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

Jamesjamesian answered 19/4, 2017 at 19:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.