C#使用HttpWebRequest与HttpWebResponse模拟用户登录

 更新时间:2017年04月20日 14:06:32   作者:道.玄  
这篇文章主要为大家详细介绍了C#使用HttpWebRequest与HttpWebResponse模拟用户登录,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

模拟艺龙旅游网登录,供大家参考,具体内容如下

想模拟登录,首先整理一下流程

1.通过360浏览器(IE,火狐等等)F12开发人员工具抓到相关数据

2.获取验证码(拿到cookie),登录时也需要使用

3.登录

F12调出开发人员工具,输入用户名,密码登录,看我们抓到了什么信息。

Request URL:这个就是登录请求的url 
https://secure.elong.com/passport/ajax/elongLogin

方式POST
Form Data:这个是我们要POST传输的数据:

userName=xzdylyh&passwd=12313&validateCode=验证码&rememberMe=false

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

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

我使用C# 设计的winform界面

复制代码

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; //存储验证码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;//获取验证码时候获取到的cookie会附加在这个容器里面
        request.KeepAlive = true;//建立持久性连接
        //整数据
        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;

        //发送数据 using结束代码段释放
        using (Stream requestStm = request.GetRequestStream())
        {
          requestStm.Write(bytepostData, 0, bytepostData.Length);
        }

        //响应
        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 获取验证码
    public Stream getCodeStream(string codeUrl)
    {

      //验证码请求
      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();//更新验证码
    }

    //更新验证码
    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();//更新验证码
    }

    private void picValidate_Click(object sender, EventArgs e)
    {
      ReflshPicImage();//更新验证码
    }
  }
}

最后执行效果,登录的session已经成功返回。

相关文章

  • c# 使用谷歌身份验证GoogleAuthenticator的示例

    c# 使用谷歌身份验证GoogleAuthenticator的示例

    这篇文章主要介绍了c# 使用谷歌身份验证GoogleAuthenticator的示例,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下
    2021-01-01
  • 详解C#多线程编程之进程与线程

    详解C#多线程编程之进程与线程

    这篇文章主要介绍了详解C#多线程编程之进程与线程的的相关资料,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-06-06
  • C#组合函数的使用详解

    C#组合函数的使用详解

    本篇文章是对C#中的组合函数的使用进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • C# async/await任务超时处理的实现

    C# async/await任务超时处理的实现

    本文主要介绍了C# async/await任务超时处理的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-02-02
  • Unity3D实战之答题系统的实现

    Unity3D实战之答题系统的实现

    本文将用Unity3D制作一个答题系统,可以从文本文档中提取题目和分数,然后绑定到UI上,在答题的过程中,自动判断分数,自动判断正确率。感兴趣的可以学习一下
    2022-03-03
  • C#验证两个QQ头像相似度的示例代码

    C#验证两个QQ头像相似度的示例代码

    这篇文章主要介绍了c#验证两个QQ头像相似度,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-03-03
  • C#实现一个控制台的点餐系统

    C#实现一个控制台的点餐系统

    这篇文章主要为大家详细介绍了C#实现一个控制台的点餐系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-11-11
  • 绑定winform中DataGrid

    绑定winform中DataGrid

    绑定winform中DataGrid,需要的朋友可以参考一下
    2013-02-02
  • C#中的let字句应用示例

    C#中的let字句应用示例

    这篇文章主要给大家介绍了C#中的let字句,文中通过应用实例介绍的很详细,相信对大家具有一定的参考价值,有需要的朋友们下面来一起看看吧。
    2017-02-02
  • C#执行Javascript代码的几种方法总结

    C#执行Javascript代码的几种方法总结

    本篇文章主要是对C#执行Javascript代码的几种方法进行了详细的总结介绍,需要的朋友可以过来参考下,希望对大家有所帮助
    2014-01-01

最新评论