ASP.NET HttpWebRequest and HttpWebResponse

The HttpWebRequest and HttpWebResponse classes are the best choices for sending and receiving HTTP data. They support a range of useful properties.

 

Simulated Yilong travel website login

If you want to simulate login, first clear up the process

1. Capture relevant data through the F12 developer tool of 360 browser (IE, Firefox, etc.)

2. Get the verification code (get the cookie) and use it when logging in

3. Log in

-------------------------------

F12 call up the developer tool, input the user name and password to log in and see what information we have caught. (360 browser here)

Request URL: This is the url of the login request
https://secure.elong.com/passport/ajax/elongLogin
Mode POST

Form Data: This is the data we want to POST:

Username = xzdylyh & passwd = 12313 & ValidateCode = verification code & rememberme = false (note that everyone may be slightly different here)

Other important information is in Request Headers

*****************************************************************

I use winform interface designed by C

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Net;
using System.IO;
using System.Data;
namespace HTTPHELPER
{
    public  class ELOGN_LOGIN
    {

       public static CookieContainer container = null; //Store verification code cookie

        #region Sign in
        public string  requestM(string uName,string passwd,string vaildate)
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            try
            {
                request = (HttpWebRequest)HttpWebRequest.Create("https://secure.elong.com/passport/ajax/elongLogin");
                request.Method = "Post";
                request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36";
                request.AllowAutoRedirect = true;
                request.CookieContainer = container;//Obtained when obtaining the verification code cookie Will be attached to this container
                request.KeepAlive = true;//Establish a persistent connection
                //Whole data
                string postData = string.Format("userName={0}&passwd={1}&validateCode={2}&rememberMe=true", uName, passwd, vaildate);
                ASCIIEncoding encoding = new ASCIIEncoding();
                byte[] bytepostData = encoding.GetBytes(postData);
                request.ContentLength = bytepostData.Length;

                //send data  using End snippet release
                using (Stream requestStm = request.GetRequestStream())
                {
                    requestStm.Write(bytepostData, 0, bytepostData.Length);
                }

                //response
                response = (HttpWebResponse)request.GetResponse();
                string text = string.Empty;
                using (Stream responseStm = response.GetResponseStream())
                {
                    StreamReader redStm = new StreamReader(responseStm, Encoding.UTF8);
                    text = redStm.ReadToEnd();
                }

               return text;
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                return msg;
            }

        }
        #endregion 

        #region Get verification code
        public Stream getCodeStream(string codeUrl)
        {

            //Verification code request
           HttpWebRequest request = (HttpWebRequest)WebRequest.Create(codeUrl);
            request.Method = "GET";
            request.ContentType = "application/x-www-form-urlencoded";
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0.1) Gecko/20100101 Firefox/5.0.1";
            request.Accept = "image/webp,*/*;q=0.8";
            request.CookieContainer = new CookieContainer();//!Very Important.!!!
            container = request.CookieContainer;
            var c = request.CookieContainer.GetCookies(request.RequestUri);
            HttpWebResponse  response = (HttpWebResponse)request.GetResponse();
            response.Cookies = container.GetCookies(request.RequestUri);
          
          Stream stream = response.GetResponseStream();
          return stream;
        }
    }
        #endregion
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using HTTPHELPER;
namespace WindowsFormsApplication8
{
    public partial class ELONG_LOGIN_FORM : Form
    {
        public ELONG_LOGIN_FORM()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            ELOGN_LOGIN elongLogin = new ELOGN_LOGIN();
            
            var rmsg = elongLogin.requestM(txtuserName.Text,txtPassword.Text,txtVaildata.Text);
            MessageBox.Show(rmsg);
        }

        private void ELONG_LOGIN_FORM_Load(object sender, EventArgs e)
        {
            ReflshPicImage();//Update verification code
        }

        //Update verification code
        public void ReflshPicImage()
        {
            string codeUrl = "https://secure.elong.com/passport/getValidateCode";
            ELOGN_LOGIN agent = new ELOGN_LOGIN();
            Stream stmImage = agent.getCodeStream(codeUrl);
            picValidate.Image = Image.FromStream(stmImage);
        }

        private void btnReValidate_Click(object sender, EventArgs e)
        {
            ReflshPicImage();//Update verification code
        }

        private void picValidate_Click(object sender, EventArgs e)
        {
            ReflshPicImage();//Update verification code
        }
    }
}

Finally, the login session has been successfully returned.

 

Original link: https://www.cnblogs.com/yhleng/p/6728864.html

Tags: ASP.NET Windows encoding Firefox IE

Posted on Sun, 03 May 2020 12:45:25 -0700 by 2705ap