Linux中的SSL WebRequest

时间:2018-02-08 14:37:13

标签: 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执行应用程序后,问题仍然存在。

有人对下一步该做什么有任何想法/线索吗?

非常感谢。

1 个答案:

答案 0 :(得分:0)

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

非常感谢社区的时间和精力,你很棒!