java httputil_java实现HTTP请求 HttpUtil

packagecom.powerX.httpClient;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.UnsupportedEncodingException;importjava.util.Iterator;importjava.util.Map;importjava.util.Map.Entry;importjava.util.Set;importorg.apache.commons.httpclient.DefaultHttpMethodRetryHandler;importorg.apache.commons.httpclient.HttpClient;importorg.apache.commons.httpclient.HttpStatus;importorg.apache.commons.httpclient.NameValuePair;importorg.apache.commons.httpclient.methods.GetMethod;importorg.apache.commons.httpclient.methods.PostMethod;importorg.apache.commons.httpclient.params.HttpMethodParams;public classHttpClient3 {public staticString doGet(String url) {//输入流

InputStream is = null;

BufferedReader br= null;

String result= null;//创建httpClient实例

HttpClient httpClient = newHttpClient();//设置http连接主机服务超时时间:15000毫秒//先获取连接管理器对象,再获取参数对象,再进行参数的赋值

httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);//创建一个Get方法实例对象

GetMethod getMethod = newGetMethod(url);//设置get请求超时为60000毫秒

getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);//设置请求重试机制,默认重试次数:3次,参数设置为true,重试机制可用,false相反

getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, true));try{//执行Get方法

int statusCode =httpClient.executeMethod(getMethod);//判断返回码

if (statusCode !=HttpStatus.SC_OK) {//如果状态码返回的不是ok,说明失败了,打印错误信息

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

}else{//通过getMethod实例,获取远程的一个输入流

is =getMethod.getResponseBodyAsStream();//包装输入流

br = new BufferedReader(new InputStreamReader(is, "UTF-8"));

StringBuffer sbf= newStringBuffer();//读取封装的输入流

String temp = null;while ((temp = br.readLine()) != null) {

sbf.append(temp).append("\r\n");

}

result=sbf.toString();

}

}catch(IOException e) {

e.printStackTrace();

}finally{//关闭资源

if (null !=br) {try{

br.close();

}catch(IOException e) {

e.printStackTrace();

}

}if (null !=is) {try{

is.close();

}catch(IOException e) {

e.printStackTrace();

}

}//释放连接

getMethod.releaseConnection();

}returnresult;

}public static String doPost(String url, MapparamMap) {//获取输入流

InputStream is = null;

BufferedReader br= null;

String result= null;//创建httpClient实例对象

HttpClient httpClient = newHttpClient();//设置httpClient连接主机服务器超时时间:15000毫秒

httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);//创建post请求方法实例对象

PostMethod postMethod = newPostMethod(url);//设置post请求超时时间

postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);

NameValuePair[] nvp= null;//判断参数map集合paramMap是否为空

if (null != paramMap && paramMap.size() > 0) {//不为空//创建键值参数对象数组,大小为参数的个数

nvp = newNameValuePair[paramMap.size()];//循环遍历参数集合map

Set> entrySet =paramMap.entrySet();//获取迭代器

Iterator> iterator =entrySet.iterator();int index = 0;while(iterator.hasNext()) {

Entry mapEntry =iterator.next();//从mapEntry中获取key和value创建键值对象存放到数组中

try{

nvp[index]= newNameValuePair(mapEntry.getKey(),new String(mapEntry.getValue().toString().getBytes("UTF-8"), "UTF-8"));

}catch(UnsupportedEncodingException e) {

e.printStackTrace();

}

index++;

}

}//判断nvp数组是否为空

if (null != nvp && nvp.length > 0) {//将参数存放到requestBody对象中

postMethod.setRequestBody(nvp);

}//执行POST方法

try{int statusCode =httpClient.executeMethod(postMethod);//判断是否成功

if (statusCode !=HttpStatus.SC_OK) {

System.err.println("Method faild: " +postMethod.getStatusLine());

}//获取远程返回的数据

is =postMethod.getResponseBodyAsStream();//封装输入流

br = new BufferedReader(new InputStreamReader(is, "UTF-8"));

StringBuffer sbf= newStringBuffer();

String temp= null;while ((temp = br.readLine()) != null) {

sbf.append(temp).append("\r\n");

}

result=sbf.toString();

}catch(IOException e) {

e.printStackTrace();

}finally{//关闭资源

if (null !=br) {try{

br.close();

}catch(IOException e) {

e.printStackTrace();

}

}if (null !=is) {try{

is.close();

}catch(IOException e) {

e.printStackTrace();

}

}//释放连接

postMethod.releaseConnection();

}returnresult;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值