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