雷火电竞-中国电竞赛事及体育赛事平台

歡迎來(lái)到入門(mén)教程網(wǎng)!

C#教程

當(dāng)前位置:主頁(yè) > 軟件編程 > C#教程 >

C#使用HttpWebRequest與HttpWebResponse模擬用戶(hù)登錄

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊:

模擬藝龍旅游網(wǎng)登錄,供大家參考,具體內(nèi)容如下

想模擬登錄,首先整理一下流程

1.通過(guò)360瀏覽器(IE,火狐等等)F12開(kāi)發(fā)人員工具抓到相關(guān)數(shù)據(jù)

2.獲取驗(yàn)證碼(拿到cookie),登錄時(shí)也需要使用

3.登錄

F12調(diào)出開(kāi)發(fā)人員工具,輸入用戶(hù)名,密碼登錄,看我們抓到了什么信息。

Request URL:這個(gè)就是登錄請(qǐng)求的url 
https://secure.elong.com/passport/ajax/elongLogin

方式POST
Form Data:這個(gè)是我們要POST傳輸?shù)臄?shù)據(jù):

userName=xzdylyh&passwd=12313&validateCode=驗(yàn)證碼&rememberMe=false

其它一些重要信息在Request Headers中

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

我使用C# 設(shè)計(jì)的winform界面

復(fù)制代碼

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; //存儲(chǔ)驗(yàn)證碼cookie

    #region 登錄
    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;//獲取驗(yàn)證碼時(shí)候獲取到的cookie會(huì)附加在這個(gè)容器里面
        request.KeepAlive = true;//建立持久性連接
        //整數(shù)據(jù)
        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;

        //發(fā)送數(shù)據(jù) using結(jié)束代碼段釋放
        using (Stream requestStm = request.GetRequestStream())
        {
          requestStm.Write(bytepostData, 0, bytepostData.Length);
        }

        //響應(yīng)
        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 獲取驗(yàn)證碼
    public Stream getCodeStream(string codeUrl)
    {

      //驗(yàn)證碼請(qǐng)求
      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();//更新驗(yàn)證碼
    }

    //更新驗(yàn)證碼
    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();//更新驗(yàn)證碼
    }

    private void picValidate_Click(object sender, EventArgs e)
    {
      ReflshPicImage();//更新驗(yàn)證碼
    }
  }
}

最后執(zhí)行效果,登錄的session已經(jīng)成功返回。

上一篇:C#如何通過(guò)RFC連接sap系統(tǒng)

欄    目:C#教程

下一篇:C# 中的EventHandler實(shí)例詳解

本文標(biāo)題:C#使用HttpWebRequest與HttpWebResponse模擬用戶(hù)登錄

本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5780.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有