服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#使用HttpWebRequest与HttpWebResponse模拟用户登录

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

2021-12-31 13:40道.玄 C#

这篇文章主要为大家详细介绍了C#使用HttpWebRequest与HttpWebResponse模拟用户登录,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

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

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

3.登录

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

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

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界面

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

复制代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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#使用HttpWebRequest与HttpWebResponse模拟用户登录

延伸 · 阅读

精彩推荐
  • C#聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题,新手速来围观,一个通俗易懂的例子帮助大家更好的理解C#接口问题,感兴趣的小伙伴们可以参考一下...

    zenkey7072021-12-03
  • C#Unity3D UGUI实现缩放循环拖动卡牌展示效果

    Unity3D UGUI实现缩放循环拖动卡牌展示效果

    这篇文章主要为大家详细介绍了Unity3D UGUI实现缩放循环拖动展示卡牌效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参...

    诗远3662022-03-11
  • C#C#直线的最小二乘法线性回归运算实例

    C#直线的最小二乘法线性回归运算实例

    这篇文章主要介绍了C#直线的最小二乘法线性回归运算方法,实例分析了给定一组点,用最小二乘法进行线性回归运算的实现技巧,具有一定参考借鉴价值,需要...

    北风其凉8912021-10-18
  • C#浅谈C# winForm 窗体闪烁的问题

    浅谈C# winForm 窗体闪烁的问题

    下面小编就为大家带来一篇浅谈C# winForm 窗体闪烁的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C#教程网7962021-12-21
  • C#c#学习之30分钟学会XAML

    c#学习之30分钟学会XAML

    一个界面程序的核心,无疑就是界面和后台代码,而xaml就是微软为构建应用程序界面而创建的一种描述性语言,也就是说,这东西是搞界面的...

    C#教程网8812021-12-10
  • C#C# 后台处理图片的几种方法

    C# 后台处理图片的几种方法

    本篇文章主要介绍了C# 后台处理图片的几种方法,非常具有实用价值,需要的朋友可以参考下。...

    IT小伙儿10162021-12-08
  • C#C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    这篇文章主要介绍了C#实现的文件操作封装类,结合完整实例形式分析了C#封装文件的删除,移动,复制,重命名等操作相关实现技巧,需要的朋友可以参考下...

    Rising_Sun3892021-12-28
  • C#C#基础之泛型

    C#基础之泛型

    泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个新功能。接下来通过本文给大家介绍c#基础之泛型,感兴趣的朋友一起学习吧...

    方小白7732021-12-03