Him_Jalpert
Him_Jalpert

Reputation: 2516

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.

Upvotes: 48

Views: 39260

Answers (4)

Jeff Ogata
Jeff Ogata

Reputation: 57783

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

Upvotes: 67

vejay2k
vejay2k

Reputation: 121

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

Upvotes: 11

EeKay
EeKay

Reputation: 6770

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

Upvotes: 1

Kibbee
Kibbee

Reputation: 66122

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

Upvotes: -3

Related Questions