How to get status code from webclient?

You can check if the error is of type WebException and then inspect the response code; if (e.Error.GetType().Name == “WebException”) { WebException we = (WebException)e.Error; HttpWebResponse response = (System.Net.HttpWebResponse)we.Response; if (response.StatusCode==HttpStatusCode.NotFound) System.Diagnostics.Debug.WriteLine(“Not found!”); } or try { // send request } catch (WebException e) { // check e.Status as above etc.. }

POSTing JSON to URL via WebClient in C#

The question is already answered but I think I’ve found the solution that is simpler and more relevant to the question title, here it is: var cli = new WebClient(); cli.Headers[HttpRequestHeader.ContentType] = “application/json”; string response = cli.UploadString(“http://some/address”, “{some:\”json data\”}”); PS: In the most of .net implementations, but not in all WebClient is IDisposable, so of … Read more

Using CookieContainer with WebClient class

WebClient wb = new WebClient(); wb.Headers.Add(HttpRequestHeader.Cookie, “somecookie”); From Comments How do you format the name and value of the cookie in place of “somecookie” ? wb.Headers.Add(HttpRequestHeader.Cookie, “cookiename=cookievalue”); For multiple cookies: wb.Headers.Add(HttpRequestHeader.Cookie, “cookiename1=cookievalue1;” + “cookiename2=cookievalue2”);

How to get a JSON string from URL?

Use the WebClient class in System.Net: var json = new WebClient().DownloadString(“url”); Keep in mind that WebClient is IDisposable, so you would probably add a using statement to this in production code. This would look like: using (WebClient wc = new WebClient()) { var json = wc.DownloadString(“url”); }

What difference is there between WebClient and HTTPWebRequest classes in .NET?

WebClient is a higher-level abstraction built on top of HttpWebRequest to simplify the most common tasks. For instance, if you want to get the content out of an HttpWebResponse, you have to read from the response stream: var http = (HttpWebRequest)WebRequest.Create(“http://example.com”); var response = http.GetResponse(); var stream = response.GetResponseStream(); var sr = new StreamReader(stream); var … Read more

How to change the timeout on a .NET WebClient object

You can extend the timeout: inherit the original WebClient class and override the webrequest getter to set your own timeout, like in the following example. MyWebClient was a private class in my case: private class MyWebClient : WebClient { protected override WebRequest GetWebRequest(Uri uri) { WebRequest w = base.GetWebRequest(uri); w.Timeout = 20 * 60 * … Read more

How to post data to specific URL using WebClient in C#

I just found the solution and yea it was easier than I thought 🙂 so here is the solution: string URI = “http://www.myurl.com/post.php”; string myParameters = “param1=value1&param2=value2&param3=value3”; using (WebClient wc = new WebClient()) { wc.Headers[HttpRequestHeader.ContentType] = “application/x-www-form-urlencoded”; string HtmlResult = wc.UploadString(URI, myParameters); } it works like charm 🙂