C # HttpWebRequest Error for HTTPS

I am trying to enter this site https://www.virginmobile.com.au programmatically (there is a membership entry form on the right).

This form works. But when I make a POST request for the form action ( https://www.virginmobile.com.au/selfcare/MyAccount/LogoutLoginPre.jsp ), it failed.

It returns 302, and then moves to a new location, it returns 405.

This is my test1.aspx.cs code

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

Can anyone help me? Thanks in advance!

+3
source share
4 answers

302 , , POST .

, HttpWebRequest.AllowAutoRedirect = false , . URL- ( ), POST.

+5

. , script, . , , :

  • User-Agent ( , , , Firefox)
  • Referer ( URL-, , URL- )
  • Accept-Charset, Accept-Encoding, Accept-Language

. , , Fiddler, , , Firefox ( , ) ( HTTPS) , , . ( TamperData , Firefox.)

+1

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();
+1

: 405 , POST GET

0

Source: https://habr.com/ru/post/1760442/


All Articles