加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#HttpWebRequest到HTTPS失败

发布时间:2020-12-16 09:29:22 所属栏目:百科 来源:网络整理
导读:我正在尝试以编程方式登录此网站 https://www.virginmobile.com.au(右侧有会员登录表单). 那种形式有效.但是当我对表单操作(https://www.virginmobile.com.au/selfcare/MyAccount/LogoutLoginPre.jsp)发出POST请求时,它失败了. 它返回302,然后跟随到新位置,
我正在尝试以编程方式登录此网站 https://www.virginmobile.com.au(右侧有会员登录表单).

那种形式有效.但是当我对表单操作(https://www.virginmobile.com.au/selfcare/MyAccount/LogoutLoginPre.jsp)发出POST请求时,它失败了.

它返回302,然后跟随到新位置,它返回405.

这是我的代码test1.aspx.cs

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,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,*/*;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,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.
  }
}

谁能帮助我?提前致谢!

解决方法

302响应正在尝试将您重定向到另一个页面,因此问题可能是您的POST数据未被发送到重定向页面.

也许尝试设置HttpWebRequest.AllowAutoRedirect = false并捕获您获得的异常背部.然后为重定向的URL创建另一个请求(在位置响应头中指定),然后使用相同的POST数据再次发出请求.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读