亲宝软件园·资讯

展开

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

人气:0
想了解基于WebClient实现Http协议的Post与Get对网站进行模拟登陆和浏览实例的相关内容吗,在本文为您仔细讲解基于WebClient实现Http协议的Post与Get对网站进行模拟登陆和浏览的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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#程序设计有所帮助。

加载全部内容

相关教程
猜你喜欢
用户评论