Add custom header in HttpWebRequest

You use the Headers property with a string index: request.Headers[“X-My-Custom-Header”] = “the-value”; According to MSDN, this has been available since: Universal Windows Platform 4.5 .NET Framework 1.1 Portable Class Library Silverlight 2.0 Windows Phone Silverlight 7.0 Windows Phone 8.1 https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.headers(v=vs.110).aspx

C# HttpWebRequest vs WebRequest

The Create method is static, and exists only on WebRequest. Calling it as HttpWebRequest.Create might look different, but its actually compiled down to calling WebRequest.Create. It only appears to be on HttpWebRequest because of inheritance. The Create method internally, uses the factory pattern to do the actual creation of objects, based on the Uri you … Read more

Send JSON via POST in C# and Receive the JSON returned?

I found myself using the HttpClient library to query RESTful APIs as the code is very straightforward and fully async’ed. To send this JSON payload: { “agent”: { “name”: “Agent Name”, “version”: 1 }, “username”: “Username”, “password”: “User Password”, “token”: “xxxxxx” } With two classes representing the JSON structure you posted that may look like … 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

“NotSupportedException” when WebRequest is unable to find a creator for that prefix

Try this hacky workaround on this issue: private static HttpWebRequest CreateWebRequest(Uri uri) { var type = Type.GetType(“System.Net.HttpRequestCreator, System, Version=4.0.0.0,Culture=neutral, PublicKeyToken=b77a5c561934e089”); var creator = Activator.CreateInstance(type,nonPublic:true) as IWebRequestCreate; return creator.Create(uri) as HttpWebRequest; }

Setting a WebRequest’s body data

With HttpWebRequest.GetRequestStream Code example from http://msdn.microsoft.com/en-us/library/d4cek6cc.aspx string postData = “firstone=” + inputData; ASCIIEncoding encoding = new ASCIIEncoding (); byte[] byte1 = encoding.GetBytes (postData); // Set the content type of the data being posted. myHttpWebRequest.ContentType = “application/x-www-form-urlencoded”; // Set the content length of the string being posted. myHttpWebRequest.ContentLength = byte1.Length; Stream newStream = myHttpWebRequest.GetRequestStream (); newStream.Write … Read more

Receiving JSON data back from HTTP request

If you are referring to the System.Net.HttpClient in .NET 4.5, you can get the content returned by GetAsync using the HttpResponseMessage.Content property as an HttpContent-derived object. You can then read the contents to a string using the HttpContent.ReadAsStringAsync method or as a stream using the ReadAsStreamAsync method. The HttpClient class documentation includes this example: HttpClient … Read more

How to ignore the certificate check when ssl

For anyone interested in applying this solution on a per request basis, this is an option and uses a Lambda expression. The same Lambda expression can be applied to the global filter mentioned by blak3r as well. This method appears to require .NET 4.5. String url = “https://www.stackoverflow.com”; HttpWebRequest request = HttpWebRequest.CreateHttp(url); request.ServerCertificateValidationCallback += (sender, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)