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.
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.
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 .
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.
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)) }
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...
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