当前位置: 移动技术网 > IT编程>开发语言>c# > 基于WebClient实现Http协议的Post与Get对网站进行模拟登陆和浏览实例

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

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例讲述了基于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#程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网