WebClient表单提交
1 get方式获取网页
Get
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 提交表单
Post
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流方式提交数据
以byte流方式提交数据
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 上传文件
文件上传
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; }