通过SSL(https)的简单HttpWebRequest在C#下找不到404

Mar*_*ini 5 ssl httpwebrequest .net-3.5

我想对一个位于安全连接上的PHP编写的Web服务进行POST.以下代码只是我经过几个小时的试验和错误后编写的测试控制台应用程序.基本上,我发现了一些使用HttpWebRequest的不同方法,但它们都是相同的.

在Web浏览器上使用"http"测试我的URI,应该返回一个空白的html(带有空体).这在浏览器和我的代码中都可以正常工作.

我继续尝试http://www.google.com,我得到谷歌...(正如预期的那样).当我将URI从http更改为https时出现问题.在Web浏览器上使用"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;
Run Code Online (Sandbox Code Playgroud)

例外是:

System.Net.WebException:远程服务器返回错误:(404)Not Found.在System.Net.HttpWebRequest.GetResponse()

注意:这里显示的http网址是我必须使用的真实网址,它不属于我,而属于另一家公司.

如果响应没问题,这就是lcHtml应包含的内容:

<html>
<head>
 <title></title>
</head>

<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

自从我一派,StackOverflowed一个大量发布这个问题之前,我发现了一些想法.一种是添加"忽略证书"代码:

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
  };
Run Code Online (Sandbox Code Playgroud)

这似乎没有任何改变.

其他用户说SSL协议类型可能是错误的...所以我尝试使用这两个无效:

 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
 ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

更新 我回去创建了一个简单的控制台应用程序.该唯一的代码是这样的:

WebRequest req = WebRequest.Create("http://servicios.mensario.com/enviomasivo/apip/");
WebResponse resp = req.GetResponse();
Run Code Online (Sandbox Code Playgroud)

这样可行.没错.

但是,如果我将URI更改为https:

WebRequest req = WebRequest.Create("https://servicios.mensario.com/enviomasivo/apip/");
WebResponse resp = req.GetResponse();
Run Code Online (Sandbox Code Playgroud)

我收到一个错误(继续尝试).

然而, 这个 PHP代码似乎工作.我看到的唯一相关代码行是:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
Run Code Online (Sandbox Code Playgroud)

西班牙语的评论说:"有了这个,我们可以忽略SSL证书".

我认为这是关键.但是

System.Net.ServicePointManager.ServerCertificateValidationCallback…
Run Code Online (Sandbox Code Playgroud)

......东西,似乎没有同样的效果.

提前致谢.

Erv*_*ter 10

在使用fiddler和firefox进行一些实验后,您的URL似乎就成了问题.对我来说,这给了404:

https://servicios.mensario.com/enviomasivo/apip/

这不是:

https://servicios.mensario.com/enviomasivo/apip

注意,结尾斜杠.此外,您发布的php示例也没有结尾斜杠.