Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebClient.UploadData correct usage for post request

Tags:

c#

webclient

i think i am going a bit crazy, when i test this on my local webserver, it works fine when i go out to the live website, it returns a blank string instead of the data i am expecting

i am not that familiar with C#, so i just wanted to check i am doing things right. the data is just plain ascii text

 wc = new WebClient();
 wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
 response = wc.UploadData(this.urlUpdate, Encoding.ASCII.GetBytes("data=" + HttpUtility.UrlEncode(buf.ToString())));

 s = Encoding.ASCII.GetString(response);
like image 950
bumperbox Avatar asked Jul 13 '09 01:07

bumperbox


People also ask

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

var httpClient = new HttpClient(); string url = "http://stopbyte.com"; // Just a sample url string parametersJson = "{'param1':'value1','param2':'value2'}" response = await httpClient. PostAsync(url, new StringContent(parametersJson)); response. EnsureSuccessStatusCode(); string content = await response.

How do I get data from WebClient?

The WebClient class uses the WebRequest class to provide access to resources. WebClient instances can access data with any WebRequest descendant registered with the WebRequest. RegisterPrefix method. UploadString Sends a String to the resource and returns a String containing any response.

What is WebClient C#?

The WebClient class provides common methods for sending data to or receiving data from any local, intranet, or Internet resource identified by a URI. The WebClient class uses the WebRequest class to provide access to resources.


1 Answers

It really depends what you are trying to do... I'm not sure, for example, why you are url-encoding data in the body. An easier way to post key/value pairs is with UploadValues;

NameValueCollection inputs = new NameValueCollection();
string value = ...
inputs.Add("data", value);
webClient.UploadValues(address, inputs);
like image 162
Marc Gravell Avatar answered Sep 21 '22 00:09

Marc Gravell