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

ぃ、小莉子 提交于 2019-12-21 06:50:37

问题


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?


回答1:


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();


来源:https://stackoverflow.com/questions/16895484/getting-handshake-failed-unexpected-packet-format-when-using-webclient-uploa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!