47

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.

ChaosPandion
  • 77,506
  • 18
  • 119
  • 157
Him_Jalpert
  • 2,476
  • 9
  • 31
  • 55
  • Now that is an entirely different question, i do not think that it would make sense to answer that here! – H.B. Dec 01 '11 at 19:43

4 Answers4

66

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))
}
BCdotWEB
  • 1,009
  • 1
  • 14
  • 35
Jeff Ogata
  • 56,645
  • 19
  • 114
  • 127
11

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

vejay2k
  • 121
  • 1
  • 4
1

Huh? As stated on MS's website WebClient.UploadData does take the method (as a string) too right? Or am I missing something?

EeKay
  • 6,494
  • 3
  • 24
  • 24
-3

I don't think that WebClient can do it. However, you can use the HttpWebrequest class to perform a put request.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
Kibbee
  • 65,369
  • 27
  • 142
  • 182
  • The second link appears to be broken, here is a copy of the code posted by the author; https://networkprogramming.wordpress.com/2015/03/26/put-request-with-httpwebrequest/ – Fiach Reid Mar 26 '15 at 17:14
  • 3
    WebClient can indeed do it. You just have to specify the method (PUT). https://msdn.microsoft.com/en-us/library/ms144237(v=vs.110).aspx – Jamie Sep 24 '16 at 01:34