WebService interface gets data over HTTP

Source: Internet
Author: User

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingsystem.web;5 usingSystem.Net;6 usingSystem.Text;7 usingSystem.IO;8 9 namespaceTestrsaTen { One  Public classgetdatabyhttp A { -  -  the  Public Static stringDoPost (stringUrlstringdata) - { -HttpWebRequest req = getwebrequest (URL,"POST"); - byte[] PostData =Encoding.UTF8.GetBytes (data); +Stream Reqstream =req. GetRequestStream (); -Reqstream.write (PostData,0, postdata.length); + reqstream.close (); AHttpWebResponse RSP =(HttpWebResponse) req. GetResponse (); atEncoding Encoding =encoding.getencoding (RSP. CharacterSet); - returngetresponseasstring (RSP, encoding); - } -  -  Public StaticHttpWebRequest GetWebRequest (stringUrlstringmethod) - { inHttpWebRequest req =(HttpWebRequest) webrequest.create (URL); -Req. Servicepoint.expect100continue =false; toReq. ContentType ="Application/x-www-form-urlencoded;charset=utf-8"; +Req. ContentType ="Text/json"; -Req. Method =method; theReq. KeepAlive =true; *Req. UserAgent ="Guanyisoft"; $Req. Timeout =1000000;Panax NotoginsengReq. Proxy =NULL; - returnreq; the } +  A  Public Static stringgetresponseasstring (HttpWebResponse RSP, Encoding Encoding) the { +StringBuilder result =NewStringBuilder (); -Stream stream =NULL; $StreamReader reader =NULL; $ Try - { - //reads the HTTP response as a stream of characters thestream =RSP. GetResponseStream (); -Reader =NewStreamReader (stream, encoding);Wuyi //each read is not greater than 256 characters and is written to a string the Char[] buffer =New Char[ the]; - intReadbytes =0; Wu  while(readbytes = reader.) Read (Buffer,0, buffer. Length)) >0) - { AboutResult. Append (Buffer,0, readbytes); $ } - } - finally - { A //Freeing Resources + if(Reader! =NULL) reader. Close (); the if(Stream! =NULL) stream. Close (); - if(RSP! =NULL) RSP. Close (); $ } the  the returnresult. ToString (); the } the  -  in  the } the } About     
View Code

WebService interface gets data over HTTP

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.