HttpClient Implementation Request

Source: Internet
Author: User

1  /// <summary>2         ///HttpClient Implementing a GET Request (asynchronous)3         /// </summary>4         Private Static Async voiddoget ()5         {6             varURL ="http://localhost:5555/api/Test/Get?id=1";7             //Create httpclient (note incoming httpclienthandler)8             varHandler =NewHttpclienthandler {automaticdecompression =Decompressionmethods.gzip};9 Ten             using(varHTTP =NewHttpClient (handler)) One             { A                 //await an asynchronous wait for a response -                 varResponse =awaithttp. Getasync (URL); -                 //ensure HTTP Success status value the Response. Ensuresuccessstatuscode (); -  -                 //await asynchronously reads the last JSON (note that at this point the GZip has been automatically decompressed since automaticdecompression = decompressionmethods.gzip) -Console.WriteLine (awaitResponse. Content.readasstringasync ()); +             } -         } +  A         /// <summary> at         ///HttpClient Implementing put requests (Async) -         /// </summary> -         Private Static Async voidDoPut () -         { -             varUserId =6; -             varURL ="http://localhost:5555/api/put/register?userid="+userId; in  -             //set the automaticdecompression of Httpclienthandler to             varHandler =NewHttpclienthandler {automaticdecompression =Decompressionmethods.gzip}; +             //Create httpclient (note incoming httpclienthandler) -             using(varHTTP =NewHttpClient (handler)) the             { *                 //use Formurlencodedcontent to do httpcontent $                 varContent =NewFormurlencodedcontent (Newdictionary<string,string>Panax Notoginseng                 { -{"UserName","Modify Hu Jingbao"}, the{"UserEmail","[email protected]"}  +                 }); A  the                 //await an asynchronous wait for a response +  -                 varResponse =awaithttp. Putasync (URL, content); $                 //ensure HTTP Success status value $ Response. Ensuresuccessstatuscode (); -                 //await asynchronously reads the last JSON (note that at this point the GZip has been automatically decompressed since automaticdecompression = decompressionmethods.gzip) -Console.WriteLine (awaitResponse. Content.readasstringasync ()); the             } -}

HttpClient Implementation Request

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.