1 get方式获取网页
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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 == "") { var Cookie = myClient.ResponseHeaders["Set-Cookie"]; if (Cookie != null) { CookieStr = GetCookie(Cookie); result += CookieStr; } } return result;
2 Post 提交表单
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public string PostNormal(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", "application/x-www-form-urlencoded"); //普通上传类型 myClient.Headers.Add("Accept-Encoding: gzip, deflate"); myClient.Headers.Add("Cache-Control: no-cache"); if (CookieStr != "") { myClient.Headers.Add(CookieStr); } myClient.Encoding = Encoder; byte[] buffer = myClient.UploadData(Url, "POST", Encoder.GetBytes(Data)); result = Encoder.GetString(buffer); //if (CookieStr == "") //{ // CookieStr = myClient.ResponseHeaders["Set-Cookie"].ToString(); // CookieStr = GetCookie(CookieStr); //} return result; }
3 以byte流方式提交数据
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public string PostData(string Url, string Referer, byte[] 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("Content-Type: application/x-www-form-urlencoded"); //上传文件的类型 myClient.Headers.Add("Accept-Encoding: gzip, deflate"); myClient.Headers.Add("Cache-Control: no-cache"); byte[] buffer = myClient.UploadData(Url, Data); result = Encoding.UTF8.GetString(buffer); return result; }
4 上传文件
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public string PostFile(string Url, string filePath, string fileName) { 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: application/x-www-form-urlencoded"); //上传文件的类型 myClient.Headers.Add("Accept-Encoding: gzip, deflate"); myClient.Headers.Add("Cache-Control: no-cache"); byte[] buffer = myClient.UploadFile(Url,"POST", filePath); result = Encoding.UTF8.GetString(buffer); myClient.Dispose(); return result; }