Use WebRequest object to invoke Sina weather forecast < Turn >

Source: Internet
Author: User

//the address you want to requeststringURL ="http://www.cnblogs.com"; //Create a WebRequest object, WebRequest is an abstract class that defines the requirements of the request,//can be used for various requests, such as Http, FTP, and so on. //HttpWebRequest is a derived class of WebRequest, specifically for HttpSystem.Net.HttpWebRequest Request= System.Net.HttpWebRequest.Create (URL) asSystem.Net.HttpWebRequest;//the way the request is set by the method property, the default is GET//you can set the Method property to any HTTP 1.1 protocol verb:, POST, PUT, DELETE, TRACE, or OPTIONS. Request. Method ="POST"; //you can also include cookies in your request//However, you must first create a Cookie containerRequest. Cookiecontainer =NewSystem.Net.CookieContainer (); System.Net.Cookie Requestcookie=NewSystem.Net.Cookie ("Request","Requestvalue","/","localhost"); request. Cookiecontainer.add (Requestcookie); Console.WriteLine ("Please enter the request parameters:"); //Enter the data for the POST.stringInputdata =console.readline ();//stitch into the request parameter string and encode it into bytesstringPostData ="firstone="+Inputdata; ASCIIEncoding encoding=Newasciiencoding ();byte[] byte1 =encoding. GetBytes (PostData); //set the Parameter form of the requestRequest. ContentType ="application/x-www-form-urlencoded"; //sets the length of the request parameter.Request. ContentLength =byte1. Length; //get the stream that is sent to the serverSystem.IO.Stream newstream =request. GetRequestStream (); //when a request is made using the POST method, the actual parameters are streamed through the Body part of the request, in the form of a streamNewstream.write (Byte1,0, Byte1. Length); //When you are finished, close the request flow.newstream.close ();//The GetResponse method really sends the request, waiting for the server to returnSystem.Net.HttpWebResponse Response=(System.Net.HttpWebResponse) request. GetResponse (); //first get the head of the response, you can know the length or type of the returned contentConsole.WriteLine ("Content length is {0}", Response. ContentLength); Console.WriteLine ("Content type is {0}", Response. ContentType); //The cookie in response is in the cookie containerforeach(System.Net.Cookie CookieinchResponse. Cookies) {Console.WriteLine ("Name: {0}, Value: {1}", cookies. Name, Cookie. Value);} Console.WriteLine (); //you can then get the content of the response in the form of a streamSystem.IO.Stream Receivestream=Response.     GetResponseStream (); //You can also wrap a byte stream into a high-level character stream for easy reading of text content//need to be aware of codingSystem.IO.StreamReader Readstream=NewSystem.IO.StreamReader (Receivestream, Encoding.UTF8); Console.WriteLine ("Response Stream received."); Console.WriteLine (Readstream.readtoend ()); //close the character stream when you are done, and the byte stream underlying the stream will automatically closeResponse. Close (); Readstream.close ();

Use WebRequest object to invoke Sina weather forecast

 Public stringGetWeather (stringCity ) {        stringweatherhtml =string.        Empty; //converting the encoding type of an input parameter        stringCityinfo = Httputility.urlencode (city,system.text.unicodeencoding.getencoding ("GB2312")); //Initializes a new WebrequstHttpWebRequest weatherrequest = (HttpWebRequest) webrequest.create ("http://php.weather.sina.com.cn/search.php?city="+cityinfo); HttpWebResponse Weatherresponse=(HttpWebResponse) weatherrequest.getresponse (); //returning data streams from Internet resourcesStream Weatherstream =Weatherresponse.getresponsestream (); //Read Data streamStreamReader Weatherstreamreader =NewStreamReader (Weatherstream,system.text.encoding.default); //reading Dataweatherhtml =Weatherstreamreader.readtoend ();        Weatherstreamreader.close ();        Weatherstream.close ();        Weatherresponse.close (); //View HTML source files for different Web sites        returnweatherhtml; }

Use WebRequest object to invoke Sina weather forecast < Turn >

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.