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.
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.
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.
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.
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");
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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With