c# - Linux 中的 SSL WebRequest

标签 c# linux ssl mono webrequest

我在开发 C# 应用程序时遇到了一些问题,该应用程序使用 WebRequest 通过 POST 方法将一些数据发送到服务器中的远程 PHP 脚本。

我的代码如下:

string url = "https://myserver.com/myfolder/myscript.php";
string PostDataQuery = "q=this+is+a+demo";

System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
System.Net.HttpWebResponse response = null;
Uri aUri = new Uri(url);
string BaseURL_Domain = aUri.GetLeftPart(UriPartial.Authority);
string BaseURL = this.EstandaritzaURL(BaseURL_Domain);
try {
    request.ProtocolVersion = System.Net.HttpVersion.Version10;
    request.KeepAlive = false;
    request.AllowAutoRedirect = true;
    request.AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate;
    request.Proxy = null;
    request.Timeout = 10000;
    request.ReadWriteTimeout = 4000;
    request.MaximumAutomaticRedirections = 3;
    request.ServicePoint.Expect100Continue = false;
    request.UseDefaultCredentials = true;
    request.Host = BaseURL;
    request.UserAgent = "Mozilla/5.0 (" + Environment.OSVersion.ToString() + ") " + Application.ProductName + " " + Application.ProductVersion;
    request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
    //req.Referer = "";
    request.Headers.Add("Accept-Language", "ca,es;q=0.7,en;q=0.3");
    request.Headers.Add("Accept-Encoding", "gzip, deflate");
    request.Headers.Add("DNT", "1");

    if (method.ToLower().Trim() == "post")
    {
        //POST
        request.ContentType = "application/x-www-form-urlencoded";
        request.Method = "POST";

        byte[] PostData = System.Text.Encoding.ASCII.GetBytes(PostDataQuery);
        request.ContentLength = PostData.Length;

        System.IO.Stream postStream = request.GetRequestStream();
        postStream.Write(PostData, 0, PostData.Length);
        postStream.Flush();
        postStream.Close();
    }

} catch (Exception e) {
    MessageBox.Show(this, "HTTP request error: " + e.Message, "HTTP request error", MessageBoxButtons.OK, MessageBoxIcon.Information);
    return false;
}
try {
    response = (System.Net.HttpWebResponse)request.GetResponse();
    if (response == null) return false;
} catch (Exception e) {
    if (e.Message.Contains("404"))
        MessageBox.Show(this, "HTTP request error: " + e.Message + Environment.NewLine + "Page not found: " + url, "Error 404", MessageBoxButtons.OK, MessageBoxIcon.Information);
    return false;
}

而且我还有接受证书的回调:

public bool HTTPAcceptCertificate(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
    return true;
}

同样的代码在 Windows 中运行良好,但是当移植到 Linux (Mint) 时,它在以下行中超时:

postStream.Write(PostData, 0, PostData.Length);

如果我使用 HTTP 而不是 HTTPS,一切正常。 我不确定这里发生了什么。本来以为是权限相关的问题,结果通过SUDO执行应用后,问题依旧。

有人知道下一步该做什么吗?

非常感谢。

最佳答案

经过两天的努力,现在它已经修复了。 解决此问题的方法是将 Mono 从 5.4.1.7 升级到版本 5.8.0.108,因此我认为我遇到了某种错误。

非常感谢社区付出的时间和精力,你们太棒了!

关于c# - Linux 中的 SSL WebRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48687999/

相关文章:

php - 如何每 3 小时在 shell 中运行相同的命令

c++ - Betfair API'ing 证书登录使用 C++ 提升 SSL 套接字

ssl - 受信任的 CA - 是如何工作的?

python 和 sll/Exchange 2007 : No suitable authentication method found

c# - .Net Core - Entity Framework - MySQL > 未找到 UseMysql

c# - 我的程序使用多少 RAM?内存分析报告 Visual Studio

c++ - 在 CodeBlocks C++ 中发现 '>' 标记之前的意外主表达式

linux - 如果第一行包含一些文本,如何删除一行中的三行? (AWK)

c# - 递归解析字符串

c# - WPF 验证错误 : How to show in separate TextBlock, 不在工具提示中