Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to do a PUT with WebClient?

with the WebClient class in .NET 4.0, is there a way to do a PUT?

I know you can do a GET with DownloadString() and a POST with UploadString(), but is there a method or property that lets you do a PUT?

Thanks.

like image 758
Him_Jalpert Avatar asked Dec 01 '11 18:12

Him_Jalpert


People also ask

What is WebClient used for?

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.

What is difference between HttpClient and WebClient?

In a nutshell, WebRequest—in its HTTP-specific implementation, HttpWebRequest—represents the original way to consume HTTP requests in . NET Framework. WebClient provides a simple but limited wrapper around HttpWebRequest. And HttpClient is the new and improved way of doing HTTP requests and posts, having arrived with .

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

There are overloads for UploadString that let you specify the method. For example, this one takes a Uri, a string for the method, and a string for the data.

using (var webClient = new WebClient()) {     webClient.UploadString(apiUrl,          WebRequestMethods.Http.Put, // or simply use "PUT"         JsonConvert.SerializeObject(payload)) } 
like image 196
Jeff Ogata Avatar answered Sep 23 '22 14:09

Jeff Ogata


You can use webclient.UploadString(urlwithparams,"Put","")

url with params should include the params in querystring format ... urlwithparams = www.foo.com?key=value&key2=value2

This worked for me...

like image 25
vejay2k Avatar answered Sep 25 '22 14:09

vejay2k