c# - 当服务器具有有效的 SSL 证书时,将 WebClient.UploadFile() 与 "Handshake failed...unexpected packet format"一起使用时获取 "https"

标签 c# visual-studio-2010 http https webclient

我正在尝试将 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/

相关文章:

安卓 9 : Cleartext HTTP traffic not permitted in webview

c# - 在 C# 中从字符串中获取数字

angularjs - 在单个 http 请求中使用与每个文件相关的元数据上传多文件

c# - 为什么这个添加 SIMD 数组的示例可能无法证明比简单的实现有任何性能提升?

asp.net - GridView - 输入字符串的格式不正确

c# - Visual Studio 在哪里寻找程序集?

c# - 无法将 MySql 添加到 C# 程序(引用和 "using"已添加)

angular - ionic 3 中的 HTTP 问题

c# - 申请路径

c# - ASP.NET 空标签文本