Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an HTTP get request with parameters

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.

like image 306
CruelIO Avatar asked Feb 05 '09 07:02

CruelIO


People also ask

Can we send parameters in GET request?

When the GET request method is used, if a client uses the HTTP protocol on a web server to request a certain resource, the client sends the server certain GET parameters through the requested URL. These parameters are pairs of names and their corresponding values, so-called name-value pairs.

Can HTTP GET have parameters?

GET requests don't have a request body, so all parameters must appear in the URL or in a header. While the HTTP standard doesn't define a limit for how long URLs or headers can be, mostHTTP clients and servers have a practical limit somewhere between 2 kB and 8 kB.

How do I GET parameters in GET request?

To use this function you just need to create two NameValueCollections holding your parameters and request headers. Add your querystring parameters (if required) as a NameValueCollection like so. NameValueCollection QueryStringParameters = new NameValueCollection(); QueryStringParameters.


2 Answers

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"); 
like image 90
wisbucky Avatar answered Sep 22 '22 17:09

wisbucky


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);         } 
like image 21
Marc Gravell Avatar answered Sep 22 '22 17:09

Marc Gravell