欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

基于WebClient实现Http协议的Post与Get对网站进行模拟登陆和浏览实例

程序员文章站 2023-12-15 18:12:16
本文实例讲述了基于webclient实现http协议的post与get对网站进行模拟登陆和浏览的方法。分享给大家供大家参考。具体分析如下: 一、问题: 我们在一些场合经...

本文实例讲述了基于webclient实现http协议的post与get对网站进行模拟登陆和浏览的方法。分享给大家供大家参考。具体分析如下:

一、问题:

我们在一些场合经常需要模拟浏览器进行一些操作,比如模拟投票,或者模拟点击,或者web游戏外挂。

二、解决方法:

c#中封装好的webclient可以在某些要求不算高的场景实现http的post和get。具体请见如下代码:

复制代码 代码如下:
using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.net;

namespace testcnm
{
    public class webclientext
    {
        public string get(string url,string referer, encoding encoder,ref string cookiestr)
        {
            string result = "";
 
            webclient myclient = new webclient();
            myclient.headers.add("accept: */*");
            myclient.headers.add("user-agent: mozilla/4.0 (compatible; msie 7.0; windows nt 5.1; trident/4.0; .net4.0e; .net4.0c; infopath.2; .net clr 2.0.50727; .net clr 3.0.04506.648; .net clr 3.5.21022; .net clr 3.0.4506.2152; .net clr 3.5.30729; se 2.x metasr 1.0)");
            myclient.headers.add("accept-language: zh-cn");
            myclient.headers.add("content-type: multipart/form-data");
            myclient.headers.add("accept-encoding: gzip, deflate");
            myclient.headers.add("cache-control: no-cache");
            if (cookiestr != "")
            {
                myclient.headers.add(cookiestr);
            }
            myclient.encoding = encoder;
            result = myclient.downloadstring(url);
            if (cookiestr == "")
            {
                cookiestr = myclient.responseheaders["set-cookie"].tostring();
                cookiestr = getcookie(cookiestr);
            }
            return result;
        }
        public string post(string url, string referer, encoding encoder, ref string cookiestr, string data)
        {
            string result = "";
 
            webclient myclient = new webclient();
            myclient.headers.add("accept: */*");
            myclient.headers.add("user-agent: mozilla/4.0 (compatible; msie 7.0; windows nt 5.1; trident/4.0; .net4.0e; .net4.0c; infopath.2; .net clr 2.0.50727; .net clr 3.0.04506.648; .net clr 3.5.21022; .net clr 3.0.4506.2152; .net clr 3.5.30729; se 2.x metasr 1.0)");
            myclient.headers.add("accept-language: zh-cn");
            myclient.headers.add("content-type: multipart/form-data");
            myclient.headers.add("accept-encoding: gzip, deflate");
            myclient.headers.add("cache-control: no-cache");
            if (cookiestr != "")
            {
                myclient.headers.add(cookiestr);
            }
            myclient.encoding = encoder;
            result = myclient.uploadstring(url, data);
            if (cookiestr == "")
            {
                cookiestr = myclient.responseheaders["set-cookie"].tostring();
                cookiestr = getcookie(cookiestr);
            }
            return result;
        }
        private string getcookie(string cookiestr)
        {
            string result = "";
 
            string[] myarray = cookiestr.split(',');
            if (myarray.count() > 0)
            {
                result = "cookie: ";
                foreach (var str in myarray)
                {
                    string[] cookiearray = str.split(';');
                    result += cookiearray[0].trim();
                    result += "; ";
                }
                result = result.substring(0, result.length - 2);
            }
            return result;
        }
    }
}

希望本文所述对大家的c#程序设计有所帮助。

上一篇:

下一篇: