package com.tw.url.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
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.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
/**
* <p>
* HTTP公用类
* 所需包:Commons-httpclient.jar,commons-codec-1.3.jar
* 学习参见网址: https://www.ibm.com/developerworks/cn/opensource/os-cn-crawler/
* </p>
*
* @author tw 2009-07-16
*
*/
public class HttpClientUtils {
public static void main(String arg[])throws Exception {
String url = "https://www.99bill.com/webapp/receiveDrawbackAction.do";
//getDoGetURL2(url,"utf-8");//测试ok
//getDoGetURL(url,"utf-8");//测试ok
getDoPostResponseDataByURL(url, null, "utf-8", true); //测试ok
}
/**
* <p>httpClient的get请求方式</p>
* @param url = "https://www.99bill.com/webapp/receiveDrawbackAction.do";
* @param charset = ="utf-8";
* @return
* @throws Exception
*/
public static String getDoGetURL(String url, String charset)throws Exception {
HttpClient client = new HttpClient();
GetMethod method1 = new GetMethod(url);
if (null == url || !url.startsWith("http")) {
throw new Exception("请求地址格式不对");
}
// 设置请求的编码方式
if (null != charset) {
method1.addRequestHeader("Content-Type",
"application/x-www-form-urlencoded; charset=" + charset);
} else {
method1.addRequestHeader("Content-Type",
"application/x-www-form-urlencoded; charset=" + "utf-8");
}
int statusCode = client.executeMethod(method1);
if (statusCode != HttpStatus.SC_OK) {// 打印服务器返回的状态
System.out.println("Method failed: " + method1.getStatusLine());
}
// 返回响应消息
byte[] responseBody = method1.getResponseBodyAsString().getBytes(method1.getResponseCharSet());
// 在返回响应消息使用编码(utf-8或gb2312)
String response = new String(responseBody, "utf-8");
System.out.println("------------------response:"+response);
// 释放连接
method1.releaseConnection();
return response;
}
/**
* <p>httpClient的get请求方式2</p>
* @param url = "https://www.99bill.com/webapp/receiveDrawbackAction.do";
* @param charset = ="utf-8";
* @return
* @throws Exception
*/
public static String getDoGetURL2(String url, String charset)
throws Exception {
/*
* 使用 GetMethod 来访问一个 URL 对应的网页,实现步骤:
* 1:生成一个 HttpClinet 对象并设置相应的参数。
* 2:生成一个 GetMethod 对象并设置响应的参数。
* 3:用 HttpClinet 生成的对象来执行 GetMethod 生成的Get 方法。
* 4:处理响应状态码。
* 5:若响应正常,处理 HTTP 响应内容。
* 6:释放连接。
*/
/* 1 生成 HttpClinet 对象并设置参数 */
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());
String response ="";
/* 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 (Header h : headers)
System.out.println(h.getName() + "------------ " + h.getValue());
// 读取 HTTP 响应内容,这里简单打印网页内容
byte[] responseBody = getMethod.getResponseBody();// 读取为字节数组
response = new String(responseBody, charset);
System.out.println("----------response:"+response);
// 读取为 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();
}
return response;
}
/**
* <p>执行一个HTTP POST请求,返回请求响应的HTML</p>
*
* @param url 请求的URL地址
* @param params 请求的查询参数,可以为null
* @param charset 字符集
* @param pretty 是否美化
* @return 返回请求响应的HTML
*/
public static String getDoPostResponseDataByURL(String url,
Map<String, String> params, String charset, boolean pretty) {
StringBuffer response = new StringBuffer();
HttpClient client = new HttpClient();
HttpMethod method = new PostMethod(url);
//设置Http Post数据
if (params != null) {
HttpMethodParams p = new HttpMethodParams();
for (Map.Entry<String, String> entry : params.entrySet()) {
p.setParameter(entry.getKey(), entry.getValue());
}
method.setParams(p);
}
try {
client.executeMethod(method);
if (method.getStatusCode() == HttpStatus.SC_OK) {
//读取为 InputStream,在网页内容数据量大时候推荐使用
BufferedReader reader = new BufferedReader(
new InputStreamReader(method.getResponseBodyAsStream(),
charset));
String line;
while ((line = reader.readLine()) != null) {
if (pretty)
response.append(line).append(System.getProperty("line.separator"));
else
response.append(line);
}
reader.close();
}
} catch (IOException e) {
System.out.println("执行HTTP Post请求" + url + "时,发生异常!");
e.printStackTrace();
} finally {
method.releaseConnection();
}
System.out.println("--------------------"+response.toString());
return response.toString();
}
}
分享到:
相关推荐
httpclient get/post请求工具类(map参数封装),方便调用
httpclient的用法,发送get请求和post请求,设置header
一个简单的易学的 基于HttpClient 4.3发送psot及get请求,返回数据,适合初学者,适合初学者
本代码是httpclient发送get请求和post请求demo,具体描述参考博客 HttpClient发送get请求和post请求
HttpClient get、post 请求,抓取网络数据,jar包以及源码
java HttpClient 发送GET请求和带有表单参数的POST请求教程例子
NULL 博文链接:https://javasam.iteye.com/blog/2117845
HttpClientUtil工具类发送get和post请求,支持http和https,支持发送文件
http请求工具类HttpClientUtil,get,post请求(csdn)————程序
HttpClient实现POST GET和文件下载 HttpClient实现POST GET和文件下载
利用httpclient-4.3.1.jar、httpcore-4.3.jar包,很简单的用java实现httpget和httppost请求。
HttpClient(用CloseableHttpClient发送get/post请求)
MFC使用H偷偷跑Client的GET和POST方法 MFC使用H偷偷跑Client的GET和POST方法
HttpClient发送http请求(post+get)需要的jar包+内符java代码案例+注解详解(这款工具类是我精心挑选适合广大java程序猿使用)
HttpClient模拟http发送post和get请求
在Android开发中我们经常会用到网络连接功能与服务器进行数据的交互,为此Android的SDK提供了Apache的HttpClient来方便我们使用各种Http服务.这里只介绍如何使用HttpClient发起GET或者POST请求
本方法以HttpClient发送请求,并且接收返回数据 举例说明 public static String doGet(String url, Map, String> params, String charset) public static void downLoadImage(String url, String path) public ...
httpclient实现的工具类含有以下功能: 1、后台模拟发送GET请求 2、后台模拟发送POST请求 3、解决了中文乱码
HttpClient发送http请求(post+get)需要的jar包+内符java代码案例+注解详解
mfc http post 和get 请求