System.Net.Http 是微軟.net4.5中推出的HTTP 應用程序的編程接口, 微軟稱之爲「現代化的 HTTP 編程接口」, 主要提供以下內容:html
1. 用戶經過 HTTP 使用現代化的 Web Service 的客戶端組件;apache
2. 可以同時在客戶端與服務端同時使用的 HTTP 組件(好比處理 HTTP 標頭和消息), 爲客戶端和服務端提供一致的編程模型。編程
我的看來是抄襲apache http client ,目前網上用的人好像很少,我的認爲使用httpclient最大的好處是:不用本身管理cookie,只要負責寫好請求便可。api
因爲網上資料很少,這裏借登陸博客園網站作個簡單的總結其get和post請求的用法。瀏覽器
查看微軟的api能夠發現其屬性方法:http://msdn.microsoft.com/zh-cn/library/system.net.http.httpclient.aspxcookie
由其api能夠看出若是想設置請求頭只須要在DefaultRequestHeaders裏進行設置
框架
建立httpcliet能夠直接new HttpClient()函數
發送請求能夠按發送方式分別調用其方法,如get調用GetAsync(Uri),post調用PostAsync(Uri, HttpContent),其它依此類推。。。post
首先,須要說明的是,本實例環境是win7 64位+vs 2013+ .net 4.5框架。網站
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace ClassLibrary1 { public class Class1 { private static String dir = @"C:\work\"; /// <summary> /// 寫文件到本地 /// </summary> /// <param name="fileName"></param> /// <param name="html"></param> public static void Write(string fileName, string html) { try { FileStream fs = new FileStream(dir + fileName, FileMode.Create); StreamWriter sw = new StreamWriter(fs, Encoding.Default); sw.Write(html); sw.Close(); fs.Close(); }catch(Exception ex){ Console.WriteLine(ex.StackTrace); } } /// <summary> /// 寫文件到本地 /// </summary> /// <param name="fileName"></param> /// <param name="html"></param> public static void Write(string fileName, byte[] html) { try { File.WriteAllBytes(dir + fileName, html); } catch (Exception ex) { Console.WriteLine(ex.StackTrace); } } /// <summary> /// 登陸博客園 /// </summary> public static void LoginCnblogs() { HttpClient httpClient = new HttpClient(); httpClient.MaxResponseContentBufferSize = 256000; httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36"); String url = "http://passport.cnblogs.com/login.aspx"; HttpResponseMessage response = httpClient.GetAsync(new Uri(url)).Result; String result = response.Content.ReadAsStringAsync().Result; String username = "hi_amos"; String password = "密碼"; do { String __EVENTVALIDATION = new Regex("id=\"__EVENTVALIDATION\" value=\"(.*?)\"").Match(result).Groups[1].Value; String __VIEWSTATE = new Regex("id=\"__VIEWSTATE\" value=\"(.*?)\"").Match(result).Groups[1].Value; String LBD_VCID_c_login_logincaptcha = new Regex("id=\"LBD_VCID_c_login_logincaptcha\" value=\"(.*?)\"").Match(result).Groups[1].Value; //圖片驗證碼 url = "http://passport.cnblogs.com" + new Regex("id=\"c_login_logincaptcha_CaptchaImage\" src=\"(.*?)\"").Match(result).Groups[1].Value; response = httpClient.GetAsync(new Uri(url)).Result; Write("amosli.png", response.Content.ReadAsByteArrayAsync().Result); Console.WriteLine("輸入圖片驗證碼:"); String imgCode = "wupve";//驗證碼寫到本地了,須要手動填寫 imgCode = Console.ReadLine(); //開始登陸 url = "http://passport.cnblogs.com/login.aspx"; List<KeyValuePair<String, String>> paramList = new List<KeyValuePair<String, String>>(); paramList.Add(new KeyValuePair<string, string>("__EVENTTARGET", "")); paramList.Add(new KeyValuePair<string, string>("__EVENTARGUMENT", "")); paramList.Add(new KeyValuePair<string, string>("__VIEWSTATE", __VIEWSTATE)); paramList.Add(new KeyValuePair<string, string>("__EVENTVALIDATION", __EVENTVALIDATION)); paramList.Add(new KeyValuePair<string, string>("tbUserName", username)); paramList.Add(new KeyValuePair<string, string>("tbPassword", password)); paramList.Add(new KeyValuePair<string, string>("LBD_VCID_c_login_logincaptcha", LBD_VCID_c_login_logincaptcha)); paramList.Add(new KeyValuePair<string, string>("LBD_BackWorkaround_c_login_logincaptcha", "1")); paramList.Add(new KeyValuePair<string, string>("CaptchaCodeTextBox", imgCode)); paramList.Add(new KeyValuePair<string, string>("btnLogin", "登 錄")); paramList.Add(new KeyValuePair<string, string>("txtReturnUrl", "http://home.cnblogs.com/")); response = httpClient.PostAsync(new Uri(url), new FormUrlEncodedContent(paramList)).Result; result = response.Content.ReadAsStringAsync().Result; Write("myCnblogs.html",result); } while (result.Contains("驗證碼錯誤,麻煩您從新輸入")); Console.WriteLine("登陸成功!"); //用完要記得釋放 httpClient.Dispose(); } public static void Main() { LoginCnblogs(); } }
代碼分析:
首先,從Main函數開始,調用LoginCnblogs方法;
其次,使用GET方法:
HttpResponseMessage response = httpClient.GetAsync(new Uri(url)).Result; String result = response.Content.ReadAsStringAsync().Result;
再者,使用POST方法:
List<KeyValuePair<String, String>> paramList = new List<KeyValuePair<String, String>>(); paramList.Add(new KeyValuePair<string, string>("__EVENTTARGET", "")); .... response = httpClient.PostAsync(new Uri(url), new FormUrlEncodedContent(paramList)).Result; result = response.Content.ReadAsStringAsync().Result;
最後,注意其返回值能夠是string,也能夠是byte[],和stream的方式,這裏看你須要什麼吧。
1).使用瀏覽器登陸後的截圖:
2).使用Httpcliet登陸後的截圖:
總結,能夠發現C#中HttpClient的用法和Java中很是類似,因此,說其抄襲確實不爲過。