Java Implementation Web Landing

Source: Internet
Author: User
Tags connect readline client stringbuffer
Web Web Landing is nothing more than Web Capture, cookie management, post and get-mode simulations.





1. Web content Acquisition


Java.io.InputStream in;


Java.net.URL URL = new Java.net.URL (www.xyz.com/content.html);


java.net.HttpURLConnection connection = (java.net.HttpURLConnection)


url.openconnection ();


connection = (java.net.HttpURLConnection) url.openconnection ();


//analog into IE


connection.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 6.0; Windows 2000) ");


Connection.connect ();


in = Connection.getinputstream ();


Java.io.BufferedReader Breader =


New BufferedReader (New InputStreamReader (in, "GBK"));


String Str=breader.readline ());


while (St!= null) {


System.out.println (str);


str=breader.readline ());


}


2.cookie Management





1. Direct Way


Get Cookie:


httpurlconnection huc= (httpurlconnection) url.openconnection ();


InputStream is = Huc.getinputstream ();


//Get SessionID.


String cookieval = Hc.getheaderfield ("Set-cookie");


String sessionId;


if (cookieval!= null)


{


sessionId = cookieval.substring (0, Cookieval.indexof (";"));


}





Send settings Cookie:


httpurlconnection huc= (httpurlconnection) url.openconnection ();


if (sessionId!= null)


{


huc.setrequestproperty ("Cookie", sessionId);


}


InputStream is = Huc.getinputstream ();











2. Jcookie Package (http://jcookie.sourceforge.net/)


Get Cookies:


url url = new URL ("http://www.site.com/");


httpurlconnection Huc = (httpurlconnection) url.openconnection ();


Huc.connect ();


InputStream is = Huc.getinputstream ();


Client client = new Client ();


Cookiejar CJ = client.getcookies (HUC);








the new request, using the cookie obtained above:





url = new URL ("http://www.site.com/");


Huc = (httpurlconnection) url.openconnection ();


client.setcookies (HUC, CJ);







Simulation
of
3.post mode

url url = new URL ("www.xyz.com");


httpurlconnection Huc = (httpurlconnection) url.openconnection ();


//settings allow output


Huc.setdooutput (TRUE);


//Set to post mode


Huc.setrequestmethod ("POST");


huc.setrequestproperty ("User-agent", "mozilla/4.7 [en]" (Win98; I) ");


stringbuffer sb = new StringBuffer ();


sb.append ("username=" +usernme);


sb.append ("&password=" +password);





Post Information


outputstream OS = Huc.getoutputstream ();


Os.write (Sb.tostring (). GetBytes ("GBK"));


Os.close ();





bufferedreader br = new BufferedReader (New InputStreamReader (Huc.getinputstream ())








Huc.connect ();





String line = Br.readline ();





while (line!= null) {





L





System.out.printli (line);








line = Br.readline ();





}

















Related Article

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.