//POST Request Public Static stringPostrequest (stringURL, httpcontent data) { varHandler =NewHttpclienthandler () {usecookies =false }; HttpClient Client=NewHttpClient (handler); varMessage =Newhttprequestmessage (httpmethod.post, URL); Message. Content=data; //message. Headers.authorization = new Authenticationheadervalue ("Bearer", Getremotetoken ()); varResponse =client. SendAsync (Message). Result; Response. Ensuresuccessstatuscode (); varresult =Response. Content.readasstringasync (). Result; returnresult; } //Send File Public Static voidSendFile (stringUrlstringPath =@"C:\<filepath>\test.txt") { using(varClient =NewHttpClient ())using(varContent =Newmultipartformdatacontent ()) {Client. BaseAddress=NewUri ("http://localhost"); varFileContent1 =Newbytearraycontent (file.readallbytes (path)); FileContent1.Headers.ContentDisposition=NewContentdispositionheadervalue ("Attachment") {FileName=path.getfilename (Path)}; Content. ADD (FILECONTENT1); varresult =client. Postasync (URL, content). Result; } }
Httpcontent type //json httpcontent content1 = new Stringcontent ("{a:1,b:2}", Encoding.UTF8, "application/ JSON "); From httpcontent content2 = new Formurlencodedcontent (new dictionary<string, string> () { "email "," 1 "}, {" pwd "," One "} });
HttpClient Encapsulation method