日期:2014-05-17 浏览次数:20714 次
import java.io.IOException;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.PostMethod;
public class HttpClientTest1 {
public static void main(String[] args) {
HttpClient client = new HttpClient();
String url = "首页地址";//得到sessionID
PostMethod method = new PostMethod(url);
method.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
try {
client.executeMethod(method);
String body = method.getResponseBodyAsString();
System.out.println(body);
Cookie[] cookie = client.getState().getCookies();
url = "提交URL";
method = new PostMethod(url);
for(int ii=0;ii<cookie.length;ii++)
{
Cookie cook = cookie[ii];
client.getState().addCookie(cook);
}
method.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
body = method.getResponseBodyAsString();
System.out.println("----"+body);
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}