代码之家  ›  专栏  ›  技术社区  ›  Aximili

对HTTPS的C#HttpWebRequest失败

  •  3
  • Aximili  · 技术社区  · 14 年前

    我正在尝试登录此网站 https://www.virginmobile.com.au 按程序(右侧有一个成员登录窗体)。

    https://www.virginmobile.com.au/selfcare/MyAccount/LogoutLoginPre.jsp )它失败了。

    它返回302,然后继续到新位置,返回405。

    这是我的代码test1。在后台

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Net;
    using System.Text;
    using System.IO;
    using System.Security.Cryptography.X509Certificates;
    using System.Net;
    
    
    public partial class test1 : System.Web.UI.Page
    {
      protected void Page_Load(object sender, EventArgs e)
      {
        string uri = "https://www.virginmobile.com.au/selfcare/MyAccount/LogoutLoginPre.jsp";
        string parameters = "username=0411222333&password=123";
    
        System.Net.ServicePointManager.CertificatePolicy = new MyPolicy();
    
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";
        //req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2 ( .NET CLR 3.0.4506.2152)";
        //req.Referer = "http://www.virginmobile.com.au/";
        //req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        req.AllowAutoRedirect = false;
    
        // Send the Post
        byte[] paramBytes = Encoding.ASCII.GetBytes(parameters);
        req.ContentLength = paramBytes.Length;
        Stream reqStream = req.GetRequestStream();
        reqStream.Write(paramBytes, 0, paramBytes.Length);   //Send it
        reqStream.Close();
    
        // Get the response
        HttpWebResponse response = (HttpWebResponse)req.GetResponse();
        if (response == null) throw new Exception("Response is null");
    
        if (!string.IsNullOrEmpty(response.Headers["Location"]))
        {
          string newLocation = response.Headers["Location"];
    
          // Request the new location
          req = (HttpWebRequest)WebRequest.Create(newLocation);
          req.Method = "POST";
          req.ContentType = "application/x-www-form-urlencoded";
          //req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2 ( .NET CLR 3.0.4506.2152)";
          //req.Referer = "http://www.virginmobile.com.au/";
          //req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
          req.AllowAutoRedirect = false;
          req.CookieContainer = new CookieContainer();
          req.CookieContainer.Add(response.Cookies);
    
          // Send the Post
          paramBytes = Encoding.ASCII.GetBytes(parameters);
          req.ContentLength = paramBytes.Length;
          reqStream = req.GetRequestStream();
          reqStream.Write(paramBytes, 0, paramBytes.Length);   //Send it
          reqStream.Close();
    
          // Get the response
          response = (HttpWebResponse)req.GetResponse(); //**** 405 Method Not Allowed here
        }
    
        StreamReader sr = new StreamReader(response.GetResponseStream());
        string responseHtml = sr.ReadToEnd().Trim();
    
        Response.Write(responseHtml);
      }
    }
    
    public class MyPolicy : ICertificatePolicy
    {
      public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem)
      {
        return true; // Return true to force the certificate to be accepted.
      }
    }
    

    有人能帮我吗?提前谢谢!

    4 回复  |  直到 14 年前
        1
  •  5
  •   Chris Fulstow    14 年前

    302响应试图将您重定向到另一个页面,因此问题可能是您的POST数据没有被发送到重定向的页面。

    或许可以试着设置 HttpWebRequest.AllowAutoRedirect = false 抓住你得到的例外 回来。然后创建另一个对重定向URL的请求(在位置响应头中指定),然后使用相同的POST数据再次发出请求。

        2
  •  1
  •   Timwi    14 年前

    • User-Agent (标识您的浏览器和版本;例如,您可以假装是Firefox)
    • Referer (标识您来自的URL;将主页URL放在此处)
    • Accept-Charset , Accept-Encoding , Accept-Language

    但可能还有其他的。您可以使用您提到的Fiddler工具来找出Firefox(或您正在使用的任何浏览器)与普通(非HTTPS)请求一起发送的头文件,然后将其中的一些头文件添加到您的请求中,看看这是否能使其正常工作。(就我个人而言 TamperData

        3
  •  1
  •   Andy Elks    14 年前

    我收到一个404错误-远程服务器返回一个错误:(404)找不到。下面是代码,我得到的错误在同一行代码,你得到的405错误。如果我用以前的版本替换代码,则返回404,但返回405错误。

    谢谢

    string uri = "https://www.virginmobile.com.au/selfcare/MyAccount/LogoutLoginPre.jsp?username=0466651800&password=160392";
    string parameters = "username=0411223344&password=123456";
    
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
    req.Method = "GET";
    req.ContentType = "application/x-www-form-urlencoded";
    //req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.2)     Gecko/20100316 Firefox/3.6.2 ( .NET CLR 3.0.4506.2152)";
    //req.Referer = "http://www.virginmobile.com.au/";
    //req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
    req.AllowAutoRedirect = false;
    
    // Send the Post
    byte[] paramBytes = Encoding.ASCII.GetBytes(parameters);
    //req.ContentLength = paramBytes.Length
    //Dim reqStream As Stream = req.GetRequestStream()
    //reqStream.Write(paramBytes, 0, paramBytes.Length)
    //Send it
    //reqStream.Close()
    
    // Get the response
    HttpWebResponse response__1 = (HttpWebResponse)req.GetResponse();
    if (response__1 == null) {
    throw new Exception("Response is null");
    }
    
    if (!string.IsNullOrEmpty(response__1.Headers("Location"))) {
    string newLocation = response__1.Headers("Location");
    
    // Request the new location
    req = (HttpWebRequest)WebRequest.Create(newLocation + "?" + parameters);
    req.Method = "GET";
    req.ContentType = "application/x-www-form-urlencoded";
    req.AllowAutoRedirect = false;
    req.CookieContainer = new CookieContainer();
    req.CookieContainer.Add(response__1.Cookies);
    
    // Send the Post
    //paramBytes = Encoding.ASCII.GetBytes(parameters)
    //req.ContentLength = paramBytes.Length
    //Dim reqStream As Stream = req.GetRequestStream()
    //reqStream.Write(paramBytes, 0, paramBytes.Length)
    //Send it
    //reqStream.Close()
    
    // Get the response
    //**** The remote server returned an error: (404) Not Found.
    response__1 = (HttpWebResponse)req.GetResponse();
    }
    
    StreamReader sr = new StreamReader(response__1.GetResponseStream());
    string responseHtml = sr.ReadToEnd().Trim();
    
        4
  •  0
  •   Aximili    14 年前