Preparatory work
1.visual Studio UPDATE3 Development environment
2.net Core 1.0.1 and above
Directory
1.HttpGet method
2.HttpPost method
3. Using the example
4. Code download
1 HttpGet
<summary>/////Use Get method to get string result (no cookie added)///</summary>//<param name= "url" > </param>//<returns></returns> public static async task<string> Httpgetasync (strin G URL, Encoding Encoding = null) {HttpClient HttpClient = new HttpClient (); var data = await httpclient.getbytearrayasync (URL); var ret = encoding. GetString (data); return ret; }///<summary>//Http Get synchronization method///</summary>//<param name= "url" ></ param>//<param name= "encoding" ></param>///<returns></returns> Public static string HttpGet (string url, Encoding Encoding = null) {HttpClient HttpClient = new HttpClient (); var t = httpclient.getbytearrayasync (URL); T.wait (); var ret = encoding. GetString (T.result); return ret; }
2 HttpPost method
<summary>//POST Async///</summary>//<param name= "url" ></param> <param name= "Poststream" ></param>//<param name= "encoding" ></param>//< ;p Aram Name= "TimeOut" ></param>///<returns></returns> public static Async Task<stri ng> httppostasync (string url, dictionary<string, string> formData = null, Encoding Encoding = null, int timeOut = 10000) {Httpclienthandler handler = new Httpclienthandler (); HttpClient client = new HttpClient (handler); MemoryStream ms = new MemoryStream (); Formdata.fillformdatastream (MS);//fill formData httpcontent HC = new Streamcontent (MS); Client. DEFAULTREQUESTHEADERS.ACCEPT.ADD (New Mediatypewithqualityheadervalue ("text/html")); Client. DEFAULTREQUESTHEADERS.ACCEPT.ADD (New Mediatypewithqualityheadervalue ("aPplication/xhtml+xml ")); Client. DEFAULTREQUESTHEADERS.ACCEPT.ADD (New Mediatypewithqualityheadervalue ("Application/xml", 0.9)); Client. DEFAULTREQUESTHEADERS.ACCEPT.ADD (New Mediatypewithqualityheadervalue ("IMAGE/WEBP")); Client. DEFAULTREQUESTHEADERS.ACCEPT.ADD (New Mediatypewithqualityheadervalue ("*/*", 0.8)); Hc. Headers.add ("useragent", "mozilla/5.0" (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/31.0.1650.57 safari/537.36 "); Hc. Headers.add ("Timeout", timeout.tostring ()); Hc. Headers.add ("KeepAlive", "true"); var r = await client. Postasync (URL, hc); byte[] tmp = await r.content.readasbytearrayasync (); return encoding. GetString (TMP); }///<summary>//POST sync///</summary>//<param name= "url" ></param& Gt <param name= "Poststream" ></param>///<param name= "encoding" ></param>//<param name= "TimeOut" ></param>///<returns></returns> public Stati C string httppost (string url, dictionary<string, string> formData = null, Encoding Encoding = null, int timeOut = 10 {Httpclienthandler handler = new Httpclienthandler (); HttpClient client = new HttpClient (handler); MemoryStream ms = new MemoryStream (); Formdata.fillformdatastream (MS);//fill formData httpcontent HC = new Streamcontent (MS); Client. DEFAULTREQUESTHEADERS.ACCEPT.ADD (New Mediatypewithqualityheadervalue ("text/html")); Client. DEFAULTREQUESTHEADERS.ACCEPT.ADD (New Mediatypewithqualityheadervalue ("Application/xhtml+xml")); Client. DEFAULTREQUESTHEADERS.ACCEPT.ADD (New Mediatypewithqualityheadervalue ("Application/xml", 0.9)); Client. DEFAULTREQUESTHEADERS.ACCEPT.ADD (New Mediatypewithqualityheadervalue ("IMAGE/WEBP")); Client. DefaultrequesthEaders. Accept.add (New Mediatypewithqualityheadervalue ("*/*", 0.8)); Hc. Headers.add ("useragent", "mozilla/5.0" (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/31.0.1650.57 safari/537.36 "); Hc. Headers.add ("Timeout", timeout.tostring ()); Hc. Headers.add ("KeepAlive", "true"); var t = client. Postasync (URL, hc); T.wait (); var t2 = T.result.content.readasbytearrayasync (); return encoding. GetString (T2. Result); }///<summary>///Assembly QueryString method//parameter with & connection, first no sign, such as: a=1&b=2&c=3///& lt;/summary>//<param name= "FormData" ></param>///<returns></returns> P Ublic static string getquerystring (this dictionary<string, string> formData) {if (FormData = = nul L | | Formdata.count = = 0) {return ""; } StringBuilder SB = new StringbuIlder (); var i = 0; foreach (var kv in formData) {i++; Sb. AppendFormat ("{0}={1}", Kv. Key, KV. Value); if (I < Formdata.count) {sb. Append ("&"); }} return sb. ToString (); }///<summary>///fill in the form information stream///</summary>//<param name= "FormData" > </param>//<param name= "stream" ></param> public static void Fillformdatastream (this Dict Ionary<string, string> FormData, Stream stream) {string datastring = GetQueryString (FormData); var formdatabytes = FormData = = null? New Byte[0]: Encoding.UTF8.GetBytes (datastring); Stream. Write (formdatabytes, 0, formdatabytes.length); Stream. Seek (0, Seekorigin.begin);//Set pointer read position}
3 Example
Modify the index.cshtml file under view
@{ viewdata["Title"] = "Home page"; Layout = null;} @{ @*var ret1=await Httpclientdemo. Httphelper.httpgetasync ("Http://www.baidu.com", System.Text.Encoding.GetEncoding ("Utf-8")); @Html. Raw (ret1) *@ @*var Ret2 = Httpclientdemo. Httphelper.httpget ("Http://www.baidu.com", System.Text.Encoding.GetEncoding ("Utf-8")); @Html. Raw (Ret2) *@ var ret3 = Httpclientdemo. Httphelper.httppost ("http://www.baidu.com", NULL, System.Text.Encoding.GetEncoding ("Utf-8")); @Html. Raw (ret3) @*var ret4 = await httpclientdemo. Httphelper.httppostasync ("http://www.baidu.com", NULL, System.Text.Encoding.GetEncoding ("Utf-8"), 10000); @Html. Raw (RET4) *@}
4 Code Download
Link: Http://pan.baidu.com/s/1gfdqwDX Password: DXZP
Synchronous asynchronous method for post and get using httpclient in ASP. NET Core