HttpClient objects can also implement network requests
HttpClient operations are simpler and more powerful than HttpWebRequest objects
HttpClient provides a series of simpler APIs to implement basic requests
Supports both authentication and asynchronous operations
Note that there are two HttpClient types in the Windows Runtime platform, which are called almost the same way, and the following uses the HttpClient in Windows.Web.Http
Send data Format
Httpformurlencodedcontent
Httpmultipartcontent
Httpmultipartformdatacontent
Httpbuffercontent
Httpstreamcontent
Httpstringcontent
Set Cookies:client. Defaultrequestheaders.add ("Cookie", "cookie_key1=cookievalue1; cookie_key2=cookievalue2; ");
1 < grid > 2 x:name = "Txturl" /> 3 < button content = "Down" Click = "Button_Click" /> Span style= "color: #008080;" >4 </ grid >
1 protected Async Override voidonnavigatedto (NavigationEventArgs e)2 {3HttpClient client =NewHttpClient ();4Client. Defaultrequestheaders.authorization =NewWindows.Web.Http.Headers.HttpCredentialsHeaderValue ("Basic","111:222");5 //GET Request6 varresult =awaitClient. Getstringasync (NewUri ("http://localhost:7080/index.ashx"));7 8 varDict =Newdictionary<string,string>();9Dict. ADD ("Ke1","Val1");TenDict. ADD ("Ke2","Val2"); One //await the client. Postasync (The New Uri ("http://localhost: 7080/index.ashx "), New Httpformurlencodedcontent (Dict)); A //await the client. Postasync (The New Uri ("http://localhost: 7080/index.ashx "), New Httpstringcontent (" abc ")); - - varFile =awaitApplicationData.Current.LocalCacheFolder.CreateFileAsync ("1.txt", creationcollisionoption.replaceexisting); the awaitFileio.appendtextasync (file,"abcdefghijklmnopqrstuvwxyz"); - varFileStream =awaitfile. OpenAsync (fileaccessmode.read); - awaitClient. Postasync (NewUri ("http://localhost:7080/index.ashx"),Newhttpstreamcontent (FileStream)); - } + - Private Async voidButton_Click (Objectsender, RoutedEventArgs e) + { A //define request URI at varRequestUri =NewUri (txturl.text); - //Create an HTTP request client HttpClient - varClient =NewHttpClient (); - //To create a periodic watch object -iprogressNewProgress - { in //here the parameter p, you can get information about the progress -System.Diagnostics.Debug.WriteLine (p.bytesreceived +"/"+p.totalbytestoreceive); to }); + //adding progress monitoring to an asynchronous task -Httpresponsemessage response =awaitclient. Getasync (RequestUri). AsTask (progress); the}
Windows Phone 16, HttpClient