程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 利用WebClient實現對Http協議的Post和Get對網站進行模擬登陸和浏覽,webclientpost

利用WebClient實現對Http協議的Post和Get對網站進行模擬登陸和浏覽,webclientpost

編輯:C#入門知識

利用WebClient實現對Http協議的Post和Get對網站進行模擬登陸和浏覽,webclientpost


我們在一些場合經常需要模擬浏覽器進行一些操作,比如模擬投票,或者模擬點擊,或者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; 

        } 

    } 

}   

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved