Getting “Handshake failed…unexpected packet format” when using WebClient.UploadFile() with “https” when the server has a valid SSL certificate

You have to make sure the port you are connecting to is port 443 instead of port 80. Example of explicitly setting the port to be used in the URL: var request = (HttpWebRequest) WebRequest.Create(“https://example.com:443/”); request.Method = “GET”; request.UserAgent = “example/1.0”; request.Accept = “*/*”; request.Host = “example.com”; var resp = (HttpWebResponse) request.GetResponse();

Uncompressing gzip response from WebClient

The easiest way to do this is to use the built in automatic decompression with the HttpWebRequest class. var request = (HttpWebRequest)HttpWebRequest.Create(“http://stackoverflow.com”); request.Headers.Add(HttpRequestHeader.AcceptEncoding, “gzip,deflate”); request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; To do this with a WebClient you have to make your own class derived from WebClient and override the GetWebRequest() method. public class GZipWebClient : WebClient … Read more

Is it possible to transfer authentication from Webbrowser to WebRequest

If the question is only “How to transfer Webbrowser authentication information to Webrequest or Webclient?” this code is enough: You can call the GetUriCookieContainer method that returns you a CookieContainer that can be used for subsequent call with WebRequest object. [DllImport(“wininet.dll”, SetLastError = true)] public static extern bool InternetGetCookieEx( string url, string cookieName, StringBuilder cookieData, … Read more

Console App Terminating Before async Call Completion

Yes. Use a ManualResetEvent, and have the async callback call event.Set(). If the Main routine blocks on event.WaitOne(), it won’t exit until the async code completes. The basic pseudo-code would look like: static ManualResetEvent resetEvent = new ManualResetEvent(false); static void Main() { CallAsyncMethod(); resetEvent.WaitOne(); // Blocks until “set” } void DownloadDataCallback() { // Do your … Read more

How to use Fiddler to debug traffic from Any app (eg. C#/WPF app)

I found the solution at this fiddler2.com page Why don’t I see traffic sent to http://localhost or http://127.0.0.1? Internet Explorer and the .NET Framework are hardcoded not to send requests for Localhost through any proxies, and as a proxy, Fiddler will not receive such traffic. The simplest workaround is to use your machine name as … Read more

How to specify SSL protocol to use for WebClient class

From the suggested other questions, I was able to solve it by adding the following line to my code: System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; This disabled TLS 1.0 from the client, and then the server accepted the connection. Hope this helps someone else with the same issue. Although the answer is similar to those other … Read more