. NET Access interface

Source: Internet
Author: User

Previously only called the interface of its own writing, this is called the external interface

I. Creating a method Link interface

1   Public Static stringHttpWebRequest (stringUrlstringData, Encoding encode,stringContentType ="application/x-www-form-urlencoded", Cookiecontainer Cookiecontainer =NULL,stringUserAgent =NULL, WebProxy proxy =NULL,intTimeOut =60000,stringMETHOD ="Get",stringtoken =NULL)2          {3             if(string. IsNullOrEmpty (URL)) {return string. Empty; }4             stringresult =string. Empty;5System.Net.HttpWebRequest webRequest = System.Net.WebRequest.Create (URL) asSystem.Net.HttpWebRequest;//Create HttpWebRequest6             Try7             {8Webrequest.method = Method;//transmission mode Get/post9Webrequest.timeout = timeout;/Gets or sets the time-out value (in milliseconds) for the GetResponse and GetRequestStream methods. TenWebrequest.contenttype = ContentType;//Content Type OneWebrequest.accept ="text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";//Gets or sets the value of the Accept HTTP header.  AWebrequest.keepalive =true; -Webrequest.useragent =useragent; -  the                 //****************** -                 //add headers, typically used for interface-side validation -WEBREQUEST.HEADERS.ADD ("UserName","Tingzi Unique"); -                 //****************** +  -Webrequest.contentlength =0;//Content Length +                 if(!string. Isnullorwhitespace (token)) AWEBREQUEST.HEADERS.ADD ("X-api-auth", token); at                 if(Proxy! =NULL) -Webrequest.proxy =proxy; -                 if(Cookiecontainer! =NULL) -                 { -Webrequest.cookiecontainer =Cookiecontainer; -                 } in                 if(!string. IsNullOrEmpty (data)) -                 { to                     byte[] buffer = encode. GetBytes (data);//Conversions +Webrequest.contentlength =buffer. Length; -                     //Write commit data the                     using(System.IO.Stream newstream =Webrequest.getrequeststream ()) *                     { $Newstream.write (Buffer,0, buffer. Length);//Write ParametersPanax Notoginseng Newstream.flush (); -                     } the                 } +  A                 //Submit Request the                 using(System.Net.HttpWebResponse response =(System.Net.HttpWebResponse) webrequest.getresponse ()) +                 { -                     if(Cookiecontainer! =NULL) $                     { $Response. Cookies =cookiecontainer.getcookies (response. ResponseUri); -                     } -                     //determines whether to return compressed information the                     if(Response. Contentencoding.tolower (). Contains ("gzip")) -                     {Wuyi                         using(GZipStream stream =NewGZipStream (response. GetResponseStream (), compressionmode.decompress)) the                         { -                             using(StreamReader Sreader =NewStreamReader (stream)) Wu                             { -result =Sreader. ReadToEnd (); About                             } $                         } -                     } -                     Else -                     { A                         using(Stream stream =Response. GetResponseStream ()) +                         { the                             using(StreamReader reader =NewStreamReader (stream, encode)) -                             { $result =Reader. ReadToEnd (); the                             } the                         } the                     } the                 } -             } in              Catch(WebException ex) the             { the //this will see the error details returned, if you upload the project do not write the direct output error. About Loghelper.writeerrorlog (ex); the                 varHttperrresponse =((HttpWebResponse) ex. Response); the                 using(varstream =Httperrresponse.getresponsestream ()) the                 { +                     if(Stream! =NULL) -                     { the                         using(varReader =NewStreamReader (stream))Bayi                         { theresult =Reader. ReadToEnd (); the                         } -                     } -                 } the             } the             returnHttputility.htmldecode (result);//returns the return value of the calling interface the}

Ps:httpwebreq Class Details: Https://msdn.microsoft.com/zh-cn/library/system.net.httpwebrequest (v=vs.110). aspx

Two. Calling methods

1   Public stringgetuserbalance (MemberInfo model)//What you need to pass this parameter2           {3Loghelper.writelog (string. Format ("tingzi_unique:{0}", DateTime.Now));//Write Log4              stringPostURL ="Interface Address";5              stringJsonstr ="{\ "username\": \ ""+ model. UserName +"\"}";//JSON type parameter, parameter write here6              stringRetdata =string. Empty;7Retdata = Utilshelper.httpwebrequest (PostURL, Jsonstr, Encoding.UTF8, ContentType:"Application/json", Method:"POST");//Calling Methods8            //now get to the retdata want to judge what to judge what9              return "return what you want to return! ";Ten}

PS: A little piece of cake please advise

. NET Access interface

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.