48

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.

1
  • Now that is an entirely different question, i do not think that it would make sense to answer that here!
    – brunnerh
    Commented Dec 1, 2011 at 19:43

4 Answers 4

67

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))
}
2
  • can u give any example?
    – Shomaail
    Commented Mar 19, 2018 at 12:54
  • 1
    client.UploadString(apiUrl, "PUT", JsonConvert.SerializeObject(payload))
    – Mike Cole
    Commented Dec 17, 2019 at 17:27
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...

1
  • Does the PUT should be in all capital? secondly the last parameter in the uploadstring is "" why?
    – Shomaail
    Commented Mar 19, 2018 at 13:06
1

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

-3

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

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.