Adjusting HttpWebRequest Connection Timeout in C#

I believe that the problem is that the WebRequest measures the time only after the request is actually made. If you submit multiple requests to the same address then the ServicePointManager will throttle your requests and only actually submit as many concurrent connections as the value of the corresponding ServicePoint.ConnectionLimit which by default gets the … Read more

Pure JavaScript Send POST Data Without a Form

You can send it and insert the data to the body: var xhr = new XMLHttpRequest(); xhr.open(“POST”, yourUrl, true); xhr.setRequestHeader(‘Content-Type’, ‘application/json’); xhr.send(JSON.stringify({ value: value })); By the way, for get request: var xhr = new XMLHttpRequest(); // we defined the xhr xhr.onreadystatechange = function () { if (this.readyState != 4) return; if (this.status == 200) … Read more

How do I use WebRequest to access an SSL encrypted site using HTTPS?

You’re doing it the correct way but users may be providing urls to sites that have invalid SSL certs installed. You can ignore those cert problems if you put this line in before you make the actual web request: ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications); where AcceptAllCertifications is defined as public bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain … Read more

send HTTP POST request in .net

There are several ways to perform HTTP GET and POST requests: Method A: HttpClient (Preferred) Available in: .NET Framework 4.5+, .NET Standard 1.1+, .NET Core 1.0+ . It is currently the preferred approach, and is asynchronous and high performance. Use the built-in version in most cases, but for very old platforms there is a NuGet … Read more

What is the difference between HTTP status code 200 (cache) vs status code 304?

The items with code “200 (cache)” were fulfilled directly from your browser cache, meaning that the original requests for the items were returned with headers indicating that the browser could cache them (e.g. future-dated Expires or Cache-Control: max-age headers), and that at the time you triggered the new request, those cached objects were still stored … 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”);

.Net HttpWebRequest.GetResponse() raises exception when http status code 400 (bad request) is returned

It would be nice if there were some way of turning off “throw on non-success code” but if you catch WebException you can at least use the response: using System; using System.IO; using System.Web; using System.Net; public class Test { static void Main() { WebRequest request = WebRequest.Create(“http://csharpindepth.com/asd”); try { using (WebResponse response = request.GetResponse()) … Read more