Forcing Basic Authentication in WebRequest

I’ve just found this very handy little chunk of code to do exactly what you need. It adds the authorization header to the code manually without waiting for the server’s challenge. public void SetBasicAuthHeader(WebRequest request, String userName, String userPassword) { string authInfo = userName + “:” + userPassword; authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo)); request.Headers[“Authorization”] = “Basic ” … Read more

Mono https webrequest fails with “The authentication or decryption has failed”

I had the same problem with Unity (which also uses mono) and this post helped me to solve it. Just add the following line before making your request: ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback; And this method: public bool MyRemoteCertificateValidationCallback(System.Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { bool isOk = true; // If there are errors in … Read more

Which versions of SSL/TLS does System.Net.WebRequest support?

This is an important question. The SSL 3 protocol (1996) is irreparably broken by the Poodle attack published 2014. The IETF have published “SSLv3 MUST NOT be used”. Web browsers are ditching it. Mozilla Firefox and Google Chrome have already done so. Two excellent tools for checking protocol support in browsers are SSL Lab’s client … Read more

How do you send an HTTP Get Web Request in Python? [duplicate]

You can use urllib2 import urllib2 content = urllib2.urlopen(some_url).read() print content Also you can use httplib import httplib conn = httplib.HTTPConnection(“www.python.org”) conn.request(“HEAD”,”/index.html”) res = conn.getresponse() print res.status, res.reason # Result: 200 OK or the requests library import requests r = requests.get(‘https://api.github.com/user’, auth=(‘user’, ‘pass’)) r.status_code # Result: 200

How to get json response using system.net.webrequest in c#?

Some APIs want you to supply the appropriate “Accept” header in the request to get the wanted response type. For example if an API can return data in XML and JSON and you want the JSON result, you would need to set the HttpWebRequest.Accept property to “application/json”. HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(requestUri); httpWebRequest.Method = WebRequestMethods.Http.Get; httpWebRequest.Accept … Read more

Cannot send a content-body with this verb-type

Don’t get the request stream, quite simply. GET requests don’t usually have bodies (even though it’s not technically prohibited by HTTP) and WebRequest doesn’t support it – but that’s what calling GetRequestStream is for, providing body data for the request. Given that you’re trying to read from the stream, it looks to me like you … 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

HttpWebRequest using Basic authentication

You can also just add the authorization header yourself. Just make the name “Authorization” and the value “Basic BASE64({USERNAME:PASSWORD})” var username = “abc”; var password = “123”; string encoded = System.Convert.ToBase64String(Encoding.GetEncoding(“ISO-8859-1”) .GetBytes(username + “:” + password)); httpWebRequest.Headers.Add(“Authorization”, “Basic ” + encoded); Edit Switched the encoding from UTF-8 to ISO 8859-1 per What encoding should I … Read more