Import java.net .*;
Import java. util. arraylist;
Import java. util. RegEx. matcher;
Import java. util. RegEx. pattern;
Import java. Io .*;
Public class questionresult {
Private Socket socket;
Private string host = "www.01hr.com ";
Private int Port = 80;
Private string filepath = "E: // index.txt ";
Private arraylist urllist, imglist;
// Create a socket Link
Public void initsocket () throws exception {
Socket = new socket (host, Port );
}
// Method 1
Public void gethttpurlcontent () throws exception {
URL url = new URL ("http://www.01hr.com ");
Inputstream input = URL. openstream ();
Bufferedreader BR = new bufferedreader (New inputstreamreader (input ));
String data;
File file = new file (filepath );
Filewriter fw = new filewriter (File );
Bufferedwriter BW = new bufferedwriter (FW );
While (Data = Br. Readline ())! = NULL ){
Matchurl (data );
Matchimage (data );
Bw. Write (data );
Bw. newline ();
}
Bw. Close ();
FW. Close ();
BR. Close ();
Input. Close ();
}
// Method 2
Public void gethttpsocketcontent () throws exception {
Stringbuffer sb = new stringbuffer ("get" + "/index.html"
+ "HTTP/1.1/R/N ");
SB. append ("Host:" + host + "/R/N ");
SB. append ("accept: */R/N ");
SB. append ("Accept-language: ZH-CN/R/N ");
SB. append ("Accept-encoding: gzip, deflate/R/N ");
SB. append ("User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)/R/N ");
SB. append ("connection: keep-alive/R/n/R/N ");
Outputstream socketout = socket. getoutputstream ();
Socketout. Write (sb. tostring (). getbytes ());
Inputstream socketin = socket. getinputstream ();
Bufferedreader BR = new bufferedreader (New inputstreamreader (socketin ));
File file = new file (filepath );
Filewriter fw = new filewriter (File );
Bufferedwriter BW = new bufferedwriter (FW );
String data;
Int I = 0;
While (Data = Br. Readline ())! = NULL ){
If (I> 9) {// The returned message is not displayed
Matchurl (data );
Matchimage (data );
Bw. Write (data );
Bw. newline ();
}
I ++;
}
Bw. Close ();
FW. Close ();
BR. Close ();
Socketin. Close ();
Socketout. Close ();
Socket. Close ();
}
// Match the link content
Public void matchurl (string Str ){
Urllist = new arraylist ();
String RegEx = "<.*? Href = /"(.*?) /"";
Pattern PAT = pattern. Compile (RegEx );
Matcher MAT = Pat. matcher (STR );
While (mat. Find ()){
Urllist. Add (mat. Group (1 ));
}
}
// Match the image content
Public void matchimage (string Str ){
Imglist = new arraylist ();
String RegEx = "Pattern PAT = pattern. Compile (RegEx );
Matcher MAT = Pat. matcher (STR );
While (mat. Find ()){
Imglist. Add (mat. Group (1 ));
}
}
// Print the matched address
Public void printcontent (string contentname, arraylist Al ){
System. Out. println (contentname );
For (INT I = 0; I <Al. Size (); I ++ ){
System. Out. println (Al. Get (I ));
}
}
Public static void main (string ARGs []) throws exception {
Questionresult client = new questionresult ();
/* Client. initsocket (); // method 2
Client. gethttpsocketcontent ();*/
Client. gethttpurlcontent (); // method 1
Client. printcontent ("--------------- print link address ---------------", client. urllist );
Client. printcontent ("--------------- print the image address -------------", client. imglist );
}
}