Windows Phone 16, HttpClient

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.