gpt4 book ai didi

c# - Linux 中的 SSL WebRequest

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:17:35 25 4
gpt4 key购买 nike

我在开发 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com