HttpWebRequest的简单通过SSL(HTTPS)给出了404未在C#发现 [英] Simple HttpWebRequest over SSL (https) gives 404 Not Found under C#

查看:1435
本文介绍了HttpWebRequest的简单通过SSL(HTTPS)给出了404未在C#发现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打一个POST,坐在一个安全的连接一个php编写的Web服务。下面code是只是一个测试控制台应用程序我几个小时的反复试验后写道。从本质上讲,我发现了一些不同的方法来使用HttpWebRequest的,但所有的人都是一样的东西。

I want to make a POST to a php-written Web Service that sits on a secure connection. The following code is just a test console app I wrote after a few hours of trial and error. Essentially, I found out a few different methods to use HttpWebRequest, but all of them are the same thing.

与Web浏览器HTTP测试我的URI,应该返回一个空的HTML(一个空的机构)。该工程确定在浏览器中的在我的code

Testing my URI with 'http' on a web browser, should return a blank html (with an empty body). This works OK in a browser and in my code.

我继续一个尝试 http://www.google.com ,我得到了谷歌...(如预期)。 当我从http URI更改为https的问题就出现了。 在 Web浏览器测试我使用的URI的https,返回相同的空白HTML(这是预料)。 但是,当我尝试在code相同的URI,我得到一个404未找​​到。

I went ahead a tried http://www.google.com and I got google… (as expected). The problem arises when I change the URI from http to https. Testing my URI with 'https' on a web browser, returns the same blank html (this is expected). But when I try the same URI in code, I get a 404 Not Found.

下面是简单的code(和URI)(取消注释第二个尝试HTTPS):

Here's the simple code (and the URI) (uncomment the second one to try 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;

唯一的例外是:

The exception is:

System.Net.WebException:远程服务器返回错误:(404)未找到。在   System.Net.HttpWebRequest.GetResponse()

System.Net.WebException: The remote server returned an error: (404) Not Found. at System.Net.HttpWebRequest.GetResponse()

注意:此处显示的HTTP URL是真正的我必须使用,它不属于我,但另一家公司

note: The http url shown here is the real one I have to use, it doesn't belong to me but to another company.

如果响应是OK,这是lcHtml应包含以下内容:

If the response were OK, this is what lcHtml should contain:

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

<body>
</body>
</html>

自从我一派, StackOverflowed 的一个很多发布这个问题之前,我发现了一些想法。一是增加了忽略证书code:

Since I googled and StackOverflowed a lot before posting this question, I found out a few ideas. One is to add the "ignore certificates" code:

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
  };

这似乎并没有改变任何东西。

That doesn't seem to change anything.

其他网友说的SSL协议类型可能是错误的,所以,我试图与这两个无济于事:

Other user said that the SSL Protocol Type might be wrong… so I tried with these two to no avail:

 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
 ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

任何想法?

更新 我回去,并创建了一个简单的控制台应用程序。 的的只有的code是这样的:

UPDATE I went back and created a simple console app. The only code is this:

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

这是工作。没有错误。

不过,如果我的URI更改为https:

However, if I change the URI to https:

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

我得到一个错误(继续前进,尝试)。

I get an error (go ahead and try it).

然而的PHP code似乎工作。唯一的相关的code线,我看到不同的是:

However this php code seems to work. The only relevant line of code that I see different is:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

注释,在西班牙语说:有了这个,我们可以忽略SSL证书

The comment, in spanish says: "With this we can ignore the SSL Cert".

我觉得这是关键。但

System.Net.ServicePointManager.ServerCertificateValidationCallback…

...的东西,似乎并不具有同样的效果。

…stuff, doesn't seem to have the same effect.

在此先感谢。

推荐答案

使用小提琴手和Firefox一些试验后,似乎您的网址是问题。对我来说,这给出了一个404:

After some experimentation using fiddler and firefox, it seems that your URL is the problem. For me, this gives a 404:

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

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

和这不:

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

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

请注意,结束斜线。此外,您发布的PHP示例没有结束斜线无论是。

Note, the ending slash. Also, the php sample you posted doesn't have the ending slash either.

这篇关于HttpWebRequest的简单通过SSL(HTTPS)给出了404未在C#发现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆