我想对位于安全连接上的 php 编写的 Web 服务进行 POST。以下代码只是我经过几个小时的反复试验后编写的测试控制台应用程序。本质上,我发现了几种不同的方法来使用 HttpWebRequest,但它们都是一样的。
在 Web 浏览器上使用“http”测试我的 URI,应返回空白 html(主体为空)。这在浏览器和我的代码中工作正常。
我继续尝试 http://www.google.com我得到了谷歌......(正如预期的那样)。 当我将 URI 从 http 更改为 https 时,问题就出现了。 在 网络浏览器 上使用“https”测试我的 URI,返回相同的空白 html(这是预期的)。 但是当我在代码中尝试相同的 URI 时,我得到了 404 Not Found。
这是简单的代码(和 URI)(取消注释第二个以尝试 https):
try
{
string lcUrl = "http://servicios.mensario.com/enviomasivo/apip/";
//string lcUrl = "https://servicios.mensario.com/enviomasivo/apip/";
// *** Establish the request
HttpWebRequest loHttp = (HttpWebRequest)WebRequest.Create(lcUrl);
// *** Set properties
loHttp.Timeout = 10000; // 10 secs
loHttp.Method = "POST"; // I added this for testing, but using GET or commenting this out doesn't change anything.
// Retrieve request info headers ******** HERE I GET THE EXCEPTION **********
HttpWebResponse loWebResponse = (HttpWebResponse)loHttp.GetResponse();
// All this code only works when lcUrl is NOT https.
Encoding enc = Encoding.GetEncoding(1252); // Windows default Code Page
StreamReader loResponseStream = new StreamReader(loWebResponse.GetResponseStream(), enc);
string lcHtml = loResponseStream.ReadToEnd();
loWebResponse.Close();
loResponseStream.Close();
}
catch ( WebException ex )
{
if ( ex.Status == WebExceptionStatus.ProtocolError )
{
HttpWebResponse response = ex.Response as HttpWebResponse;
if ( response != null )
{
// Process response
Console.WriteLine(ex.ToString());
}
}
}
Console.Read();
return;
异常(exception)是:
System.Net.WebException: The remote server returned an error: (404) Not Found. at System.Net.HttpWebRequest.GetResponse()
注意:此处显示的http url是我必须使用的真实网址,它不属于我而是属于另一家公司。
如果响应正常,这就是 lcHtml 应该包含的内容:
<html>
<head>
<title></title>
</head>
<body>
</body>
</html>
在发布这个问题之前,我用 Google 和 StackOverflow 搜索了一下,我发现了一些想法。一种是添加“忽略证书”代码:
System.Net.ServicePointManager.ServerCertificateValidationCallback += delegate( object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors )
{
return true; // **** Always accept
};
这似乎并没有改变什么。
其他用户说 SSL 协议(protocol)类型可能是错误的......所以我尝试了这两个无济于事:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
有什么想法吗?
更新 我回去创建了一个简单的控制台应用程序。 唯一的代码是这样的:
WebRequest req = WebRequest.Create("http://servicios.mensario.com/enviomasivo/apip/");
WebResponse resp = req.GetResponse();
行得通。没有错误。
但是,如果我将 URI 更改为 https:
WebRequest req = WebRequest.Create("https://servicios.mensario.com/enviomasivo/apip/");
WebResponse resp = req.GetResponse();
我收到一个错误(继续尝试)。
但是 this PHP代码似乎工作。我看到的唯一相关代码行是:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
注释,用西类牙语说:“有了这个我们可以忽略 SSL 证书”。
我认为这是关键。但是
System.Net.ServicePointManager.ServerCertificateValidationCallback…
...东西,似乎没有同样的效果。
提前致谢。
最佳答案
在使用 fiddler 和 firefox 进行一些实验后,您的 URL 似乎是问题所在。对我来说,这给出了 404:
https://servicios.mensario.com/enviomasivo/apip/
这不是:
https://servicios.mensario.com/enviomasivo/apip
注意,结束斜杠。此外,您发布的 php 示例也没有结尾斜杠。
关于.net-3.5 - 通过 SSL (https) 的简单 HttpWebRequest 在 C# 下给出 404 Not Found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/768030/