Abhishek
Abhishek

Reputation: 1379

Using WebClient.UploadStringAsync with GET data

I'm trying to use WebClient.UploadStringAsync method to send some data to server. It works fine when I send POST data, but when using GET, it throws me an error "An exception occurred during a WebClient request."
Here is my code:

WebClient client = new WebClient();
String data = "param1=value1&param2=value2";
client.UploadStringAsync(new Uri("http://somesite.com"), "GET", data);

Any idea what's going wrong?

Upvotes: 0

Views: 626

Answers (2)

Matt
Matt

Reputation: 1582

Uploading data for a GET breaks convention. You might also want to take a look at HTTPClient which you can install via NuGet.

Upvotes: 0

Den
Den

Reputation: 16826

  1. Don't use UploadStringAsync for GET. There is DownloadStringAsync designed specifically for that.

  2. Don't use WebClient because it is bound to the UI thread. Use HttpWebRequest instead.

Upvotes: 1

Related Questions