我正在尝试将 WebClient.UploadFile 与 HTTPS URL 一起使用,但我最终得到了
"System.IO.IOException: The handshake failed due to an unexpected packet format"
同样的代码在 Http 上工作得很好,但我试图访问的服务器有一个非常好的 ssl 证书。以下是与网络电话相关的任何内容:
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);
我添加的任何 HttpRequestHeaders 和使用 https 的问题是否存在?或者如果我想使用 https,我是否缺少必要的 header ?有没有人知道为什么当 SSL 证书有效时这适用于 HTTP 但不适用于 HTTPS?
最佳答案
您必须确保要连接的端口是端口 443 而不是端口 80。
明确设置要在 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();
关于c# - 当服务器具有有效的 SSL 证书时,将 WebClient.UploadFile() 与 "Handshake failed...unexpected packet format"一起使用时获取 "https",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16895484/