Package Crawl page;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import org.apache.commons.httpclient.HttpClient;
Import org.apache.commons.httpclient.HttpException;
Import Org.apache.commons.httpclient.HttpStatus;
Import Org.apache.commons.httpclient.NameValuePair;
Import Org.apache.commons.httpclient.methods.PostMethod;
public class Retrivepage {
private static HttpClient HttpClient = new HttpClient ();
public static void Main (string[] args) {
Grab the rabbit's homepage and output it
try {
Retrivepage.downloadpage ("http://www.lietu.com");
} catch (HttpException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
}
private static void Downloadpage (String path) throws HttpException, IOException {
System.out.println ("123123");
InputStream input = null;
OutputStream output = null;
Get the Post method
Postmethod Postmethod = new Postmethod (path);
Set the parameters of the Post method
namevaluepair[] PostData = new namevaluepair[2];
Postdata[0] = new Namevaluepair ("name", "Lietu");
POSTDATA[1] = new Namevaluepair ("Password", "* * * * *");
Add parameters to the request path
Postmethod.addparameters (PostData);
Execute, return status code
int statusCode = Httpclient.executemethod (Postmethod);
System.out.println (StatusCode);
if (StatusCode = = HTTPSTATUS.SC_OK) {
input = Postmethod.getresponsebodyasstream ();
Get the name of the file
String fileName = path.substring (Path.lastindexof ('/') +1);
Get the output stream of the file
System.out.println (FileName);
Output = new FileOutputStream (fileName);
Output to File
int tempbyte =-1;
while ((Tempbyte = Input.read ()) > 0) {
Output.write (Tempbyte);
}
Close Resource
if (input! = null) {
Input.close ();
}
if (output! = null) {
Output.close ();
}
}
}
}
Web crawler Java Implementation Crawl Web content