39

I am trying to use WebClient.UploadFile with a HTTPS URL but I am ending up with

"System.IO.IOException: The handshake failed due to an unexpected packet format"

The same code works perfectly fine with Http but the server that I am trying to hit has a perfectly fine ssl certificate. Here is anything relevant to the web call:

var url = WebServiceCommunication.GetProtocolName() + "..."; //turns out to be     "https://...
var wc = new WebClient();
//I am adding: 
wc.Headers.Add(HttpRequestHeader.KeepAlive, "...")
wc.Headers.Add(HttpRequestHeader.AcceptLanguage, "...")
we.Headers.Add(HttpRequestHeader.Cookie, "...")

wc.UploadFile(url, "POST", filename);

Is the issue with any of the HttpRequestHeaders I am adding AND using https with those? Or am I missing a necessary header if I want to use https? Does anyone have any pointers as to why this would work with HTTP but NOT HTTPS when the SSL cert is valid?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
user1867353
  • 487
  • 1
  • 6
  • 12
  • Similar error in [System.IO.IOException: The handshake failed due to an unexpected > packet format?](//stackoverflow.com/q/5178757) – Michael Freidgeim May 22 '17 at 08:40
  • This usually happen if you connect to a webserver using https but the web server is not set up for https. Try to use http:// instead of https:// in the URL. – nivs1978 Sep 03 '18 at 08:08

2 Answers2

46

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();
rene
  • 41,474
  • 78
  • 114
  • 152
10

You can also get this error if you're clueless like me and don't recognize that your web server project has crashed and is no longer running.

Brian Hasden
  • 4,050
  • 2
  • 31
  • 37