Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use verb GET with WebClient request?

How might I change the verb of a WebClient request? It seems to only allow/default to POST, even in the case of DownloadString.

        try         {             WebClient client = new WebClient();                            client.QueryString.Add("apiKey", TRANSCODE_KEY);             client.QueryString.Add("taskId", taskId);             string response = client.DownloadString(TRANSCODE_URI + "task");                             result = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(response);         }         catch (Exception ex )         {             result = null;             error = ex.Message + " " + ex.InnerException;         } 

And Fiddler says:

POST http://someservice?apikey=20130701-234126753-X7384&taskId=20130701-234126753-258877330210884 HTTP/1.1 Content-Length: 0 
like image 361
FlavorScape Avatar asked Jul 02 '13 00:07

FlavorScape


People also ask

What is an exception occurred during a WebClient request?

If Directory does not exist,this error message comes as 'An exception occurred during a WebClient request" Because Web Client Does not find the Folder to store downloaded files.

How do I add a custom header in WebClient?

If the number of possible headers is limited, you can declare them as public enum in your CustomWebClient class and create an overload of either the constructor or the UploadString() function (whichever one you like) and pass it the value of the enum to set the header accordingly.

How do you send parameters data using WebClient POST request in C?

var url = "https://your-url.tld/expecting-a-post.aspx" var client = new WebClient(); var method = "POST"; // If your endpoint expects a GET then do it. var parameters = new NameValueCollection(); parameters. Add("parameter1", "Hello world"); parameters. Add("parameter2", "www.stopbyte.com"); parameters.


2 Answers

If you use HttpWebRequest instead you would get more control of the call. You can change the REST verb by the Method property (default is GET)

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(HostURI); request.Method = "GET"; String test = String.Empty; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) {     Stream dataStream = response.GetResponseStream();     StreamReader reader = new StreamReader(dataStream);     test = reader.ReadToEnd();     reader.Close();     dataStream.Close();  }  DeserializeObject(test ...) 
like image 162
Niklas Bjorkman Avatar answered Sep 21 '22 18:09

Niklas Bjorkman


Not sure if you can use WebClient for that. But why not use HttpClient.GetAsync Method (String) http://msdn.microsoft.com/en-us/library/hh158944.aspx

like image 34
jeffo Avatar answered Sep 23 '22 18:09

jeffo