package com.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
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.URI;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.cookie.CookieSpec;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class HttpWeb {
public static String getGetResponse(String url) {
String html = "";
// 构造HttpClient的实例
HttpClient httpClient = new HttpClient();
// 创建GET方法的实例
GetMethod getMethod = new GetMethod(url);
// 使用系统提供的默认的恢复策略
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
try {
// 执行getMethod
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + getMethod.getStatusLine());
}
// 处理内容
html = getMethod.getResponseBodyAsString();
} catch (HttpException e) {
// 发生致命的异常,可能是协议不对或者返回的内容有问题
System.out.println("Please check your provided http address!");
e.printStackTrace();
} catch (IOException e) {
// 发生网络异常
e.printStackTrace();
} finally {
// 释放连接
getMethod.releaseConnection();
}
return html;
}
public static String getPostResponse(String url) throws HttpException, IOException {
String html = "";
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(url);
postMethod.setRequestHeader("accept", "*/*");
postMethod.setRequestHeader("connection", "Keep-Alive");
postMethod.setRequestHeader("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
postMethod.setRequestHeader("Accept-Language", "zh-cn,zh;q=0.5");
// postMethod.setRequestHeader("Accept-Encoding", "gzip,deflate");
// postMethod.setRequestHeader("Content-Type", "text/html;charset=utf-8");
// 填入各个表单域的值
NameValuePair[] data = { new NameValuePair("msg", "你好") };
// 将表单的值放入postMethod中
postMethod.setRequestBody(data);
// 执行postMethod
int statusCode = httpClient.executeMethod(postMethod);
// HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发
// 301或者302
if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
// 从头中取出转向的地址
Header locationHeader = postMethod.getResponseHeader("location");
String location = null;
if (locationHeader != null) {
location = locationHeader.getValue();
System.out.println("The page was redirected to:" + location);
} else {
System.err.println("Location field value is null.");
}
return html;
}
// html = postMethod.getResponseBodyAsString();
System.out.println(postMethod.getResponseCharSet());
// byte[] responseBody = postMethod.getResponseBody();
BufferedReader in = new BufferedReader(new InputStreamReader(postMethod.getResponseBodyAsStream(), postMethod
.getResponseCharSet()));
StringBuffer sb = new StringBuffer();
int chari;
while ((chari = in.read()) != -1) {
sb.append((char) chari);
}
html = sb.toString();
in.close();
postMethod.releaseConnection();
CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
postMethod.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
URI uri = postMethod.getURI();
Cookie[] cookies = cookiespec.match(uri.getHost(), uri.getPort() == -1 ? 80 : uri.getPort(), "/", false,
httpClient.getState().getCookies());
for (Cookie cookie : cookies) {
System.out.println(cookie.getName());
System.out.println(cookie.getValue());
}
postMethod.setURI(new URI("http://www.ssread.com/zt/writecookie2.jsp", false));
httpClient.executeMethod(postMethod);
BufferedReader in2 = new BufferedReader(new InputStreamReader(postMethod.getResponseBodyAsStream(), postMethod
.getResponseCharSet()));
StringBuffer sb2 = new StringBuffer();
int chari2;
while ((chari2 = in2.read()) != -1) {
sb2.append((char) chari2);
}
html = sb2.toString();
in2.close();
postMethod.releaseConnection();
// PostMethod postMethod2 = new PostMethod("http://www.ssread.com/zt/writecookie2.jsp");
// // httpClient.getState().addCookies(cookies);
// httpClient.executeMethod(postMethod2);
// BufferedReader in2 = new BufferedReader(new InputStreamReader(postMethod2.getResponseBodyAsStream(),
// postMethod2.getResponseCharSet()));
// StringBuffer sb2 = new StringBuffer();
// int chari2;
// while ((chari2 = in2.read()) != -1) {
// sb2.append((char) chari2);
// }
// html = sb2.toString();
// in2.close();
// postMethod2.releaseConnection();
return html;
}
public static class UTF8PostMethod extends PostMethod {
public UTF8PostMethod(String url) {
super(url);
}
@Override
public String getRequestCharSet() {
return super.getRequestCharSet();
// return "UTF-8";
}
}
public static void main(String[] args) throws HttpException, IOException {
System.out.println(HttpWeb.getPostResponse("http://www.ssread.com/zt/writecookie.jsp"));
}
}
posted on 2009-06-25 17:52
SIMONE 阅读(12936)
评论(0) 编辑 收藏 所属分类:
JAVA