Linux 中的 SSL WebRequest
SSL WebRequest in Linux
我在开发 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执行应用后,问题依旧
有人idea/clue知道下一步该怎么做吗?
非常感谢。
经过两天的努力,现在它已经修复了。
解决这个问题的方法是将 Mono 从 5.4.1.7 升级到版本 5.8.0.108,所以我认为我遇到了某种错误。
非常感谢社区付出的时间和精力,你们太棒了!
我在开发 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执行应用后,问题依旧
有人idea/clue知道下一步该怎么做吗?
非常感谢。
经过两天的努力,现在它已经修复了。 解决这个问题的方法是将 Mono 从 5.4.1.7 升级到版本 5.8.0.108,所以我认为我遇到了某种错误。
非常感谢社区付出的时间和精力,你们太棒了!