import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.httpclient.Header;
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();
static{
}
public static boolean downloadPage(String path)
throws HttpException, IOException{
InputStream input=
null;
OutputStream output=
null;
PostMethod postMethod=
new PostMethod(path);
int statusCode=httpClient.executeMethod(postMethod);
if(statusCode==HttpStatus.SC_OK){
input=postMethod.getResponseBodyAsStream();
String filename=
"G:\\pachon\\"+
"hello.html";
System.out.println(
"filtname="+filename);
output=
new FileOutputStream(filename);
int tempBytes=-
1;
while((tempBytes=input.read())>
0){
output.write(tempBytes);
}
if(input!=
null){
input.close();
}
if(output!=
null){
output.close();
}
return true;
}
if((statusCode==HttpStatus.SC_MOVED_TEMPORARILY)||
(statusCode==HttpStatus.SC_MOVED_PERMANENTLY)||
(statusCode==HttpStatus.SC_TEMPORARY_REDIRECT)||
statusCode==HttpStatus.SC_SEE_OTHER){
Header header=postMethod.getResponseHeader(
"location");
if(header==
null){
String newUrl=header.getValue();
if(newUrl==
null||newUrl.equals(
"")){
newUrl=
"/";
PostMethod redirect=
new PostMethod(newUrl);
}
}
}
return false;
}
public static void main(String[] args) {
try {
RetrivePage.downloadPage(
"http://www.lietuw.com/");
System.out.println(
"执行成功!");
}
catch (HttpException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}