您的位置:首页 > 理论基础 > 计算机网络

HttpClient

2015-09-28 16:11 591 查看
介绍下一篇上好的文章,对HttpClient有很全很详细的见解https://www.ibm.com/developerworks/cn/opensource/os-cn-crawler/

这个是里面的部分内容

/* 1 生成 HttpClinet 对象并设置参数*/

[code]HttpClient httpClient=new HttpClient();

//设置 Http 连接超时为5秒

httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);

/*2 生成 GetMethod 对象并设置参数*/

GetMethod getMethod=new GetMethod(url); 

//设置 get 请求超时为 5 秒

getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT,5000);

//设置请求重试处理,用的是默认的重试处理:请求三次

getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,

new DefaultHttpMethodRetryHandler());

/*3 执行 HTTP GET 请求*/

try{ 

int statusCode = httpClient.executeMethod(getMethod);
/*4 判断访问的状态码*/

if (statusCode != HttpStatus.SC_OK) 

{

System.err.println("Method failed: "+ getMethod.getStatusLine());

}

/*5 处理 HTTP 响应内容*/

//HTTP响应头部信息,这里简单打印

Header[] headers=getMethod.getResponseHeaders();

for(Headerh:headers)

System.out.println(h.getName()+" "+h.getValue());*/

//读取 HTTP 响应内容,这里简单打印网页内容

byte[] responseBody = getMethod.getResponseBody();//读取为字节数组

System.out.println(new String(responseBody));

//读取为 InputStream,在网页内容数据量大时候推荐使用

InputStream response = getMethod.getResponseBodyAsStream();//


}

catch (HttpException e)

{

// 发生致命的异常,可能是协议不对或者返回的内容有问题

System.out.println("Please check your provided http address!");

e.printStackTrace();

 } 

catch (IOException e)

{

// 发生网络异常

e.printStackTrace();

 } finally {

 /*6 .释放连接*/

getMethod.releaseConnection(); 

}

[/code]

其实我只是想利用它来模 拟输入用户名和口令进行登录然后访问另一个页面

但是关键问题还是怎么得到NameValuePair name = new NameValuePair("name", "ld"); 里的key和value,还是不明白

/*

[code] * Created on 2003-12-7 by Liudong

*/

package http.demo;

import org.apache.commons.httpclient.*;

import org.apache.commons.httpclient.cookie.*;

import org.apache.commons.httpclient.methods.*;
/**

* 用来演示登录表单的示例

* @author Liudong

*/

public class FormLoginDemo {

static final String LOGON_SITE = "localhost";

static final intLOGON_PORT = 8080;

 

public static void main(String[] args) throws Exception{

 HttpClient client = new HttpClient();

 client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);


 //模拟登录页面login.jsp->main.jsp

 PostMethod post = new PostMethod("/main.jsp");

 NameValuePair name = new NameValuePair("name", "ld");

 NameValuePair pass = new NameValuePair("password", "ld");

 post.setRequestBody(new NameValuePair[]{name,pass});

 int status = client.executeMethod(post);

 System.out.println(post.getResponseBodyAsString());

 post.releaseConnection();


 //查看cookie信息

 CookieSpec cookiespec = CookiePolicy.getDefaultSpec();

 Cookie[] cookies = cookiespec.match(LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies());

 if (cookies.length == 0) {

 System.out.println("None"); 

 } else {

 for (int i = 0; i < cookies.length; i++) {

System.out.println(cookies[i].toString()); 

 }

 }

 //访问所需的页面main2.jsp

 GetMethod get = new GetMethod("/main2.jsp");

 client.executeMethod(get);

 System.out.println(get.getResponseBodyAsString());

 get.releaseConnection();

}

}

[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: