Httpclien Get&post

Source: Internet
Author: User

The new company to work the next week, began to enter. Net Core, this aspect of things relatively new, so already packaged good things relatively few, such as Httpclien, such as Open source class library, find NuGet long, not found, so first write a humble to use first.

References:usingSystem.Threading.Tasks;usingSystem.Net.Http;usingNewtonsoft.json;usingSystem.Net.Http.Headers; Help class: Public Static classHttphelper { Public Static AsyncTask<t> get<t> (stringURL) {            Try            {                using(varClient =NewHttpClient ()) {                    varResponsemsg =awaitclient.                    Getasync (URL); if(responsemsg.issuccessstatuscode) {stringStrjson =awaitResponseMsg.Content.ReadAsStringAsync (); returnJsonconvert.deserializeobject<t>(Strjson); }                    Else                    {                        return default(T); }                }            }            Catch            {                return default(T); }        }         Public Static AsyncTask<t> post<t> (stringUrlDynamicpara) {            Try            {                if(Para! =NULL)                {                    varRequestjson =Jsonconvert.serializeobject (para); Httpcontent httpcontent=Newstringcontent (Requestjson); HttpContent.Headers.ContentType=NewMediatypeheadervalue ("Application/json"); using(varClient =NewHttpClient ()) {                        varResponsejson =awaitclient. Postasync (URL, httpcontent).                        Result.Content.ReadAsStringAsync (); returnJsonconvert.deserializeobject<t>(Responsejson); }                }                return default(T); }            Catch            {                return default(T); }        }    }

To invoke the test:

=======================================================//                  &nbsp.----.//                _. ' __     './/           .--(^) (^^)---/ #\//         . '  @          /###\//          :         ,   #####//            '-.. __.-'  _.-\###///                  ';_:     ' '//               . ' "" "" "" './/&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp;     /,  ya ,\\//             //Up! 409  \\//             '-._______.-'//             ___ '.  | . ' ___//            (______|______)//====================== =================================

Httpclien get&post

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.