How to make an HTTP get request with parameters

Viewed 327202

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.

6 Answers

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

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

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

GET request with multiple params:

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

Related