/**
* HttpClient业务
*
* @author
* @version 1.0
*/
public class HttpClient implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private static final int OK = 200; // OK: Success!
private static final int ConnectionTimeout = 25000;
private static final int ReadTimeout = 1200000;
private static final String DEFAULT_CHARSET = "UTF-8";
private static final String _GET = "GET";
private static final String _POST = "POST";
/**
* Get 请求
*
* @param url 请求地址
* @return 输出流对象
* @throws
*/
public Response get(String url) throws IOException {
return httpRequest(url, _GET, null);
}
/**
* Post 请求
*
* @param url 请求地址
* @param postData 提交数据
* @return 输出流对象
* @throws
*/
public Response post(String url,String postData) throws IOException {
return httpRequest(url, _POST, postData);
}
/**
/**
* 获取http请求连接
*
* @param url 连接地址
* @return http连接对象
* @throws IOException
*/
private HttpURLConnection getHttpURLConnection(String url) throws IOException {
URL urlGet = new URL(url);
HttpURLConnection con = (HttpURLConnection) urlGet.openConnection();
return con;
}
/**
* 通过http协议请求url
*
* @param url 提交地址
* @param method 提交方式
* @param postData 提交数据
* @return 响应流
* @throws
*/
private Response httpRequest(String url, String method, String postData) throws IOException {
Response res = null;
OutputStream output;
HttpURLConnection http;
//创建https请求连接
http = getHttpURLConnection(url);
//判断https是否为空,如果为空返回null响应
if (http != null) {
//设置Header信息
setHttpHeader(http, method);
//判断是否需要提交数据
if (method.equals(_POST) && null != postData) {
//讲参数转换为字节提交
byte[] bytes = postData.getBytes("UTF-8");
//设置头信息
http.setRequestProperty("Content-Length", Integer.toString(bytes.length));
//开始连接
http.connect();
//获取返回信息
output = http.getOutputStream();
output.write(bytes);
output.flush();
output.close();
} else {
//开始连接
http.connect();
}
//创建输出对象
res = new Response(http);
//获取响应代码
if (res.getStatus() == OK) {
return res;
}
}
return res;
}
private void setHttpHeader(HttpURLConnection httpUrlConnection, String method)
throws IOException {
//设置header信息
httpUrlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//设置User-Agent信息
httpUrlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36");
//设置可接受信息
httpUrlConnection.setDoOutput(true);
//设置可输入信息
httpUrlConnection.setDoInput(true);
//设置请求方式
httpUrlConnection.setRequestMethod(method);
httpUrlConnection.setConnectTimeout(ConnectionTimeout);
httpUrlConnection.setReadTimeout(ReadTimeout);
//设置编码
httpUrlConnection.setRequestProperty("Charsert", "UTF-8");
}
public static String getUrl(Map<String, String> params, String charset)
throws UnsupportedEncodingException {
StringBuffer buffer = new StringBuffer("?");
if (params == null && params.isEmpty()) {
return null;
}
// 否则的话,开始拼接需要传递的值,也就是URL?AA==BB&CC==EE这样的类似的连接值
Set<Map.Entry<String, String>> entries = params.entrySet();
for (Map.Entry<String, String> entry : entries) {
String name = entry.getKey();
String value = entry.getValue();
// 还需要进行一次判断是否为空,一定要谨慎
if (StringUtils.isNotEmpty(name) && StringUtils.isNotEmpty(value)) {
// 如果不为空的话,开始进行连接操作
buffer.append(name).append("=").append(URLEncoder.encode(value, charset)).append("&");
}
}
return buffer.toString().substring(0, buffer.toString().length() - 1);
}
}
java HttpClient Post 请求和Get 请求
最新推荐文章于 2024-08-06 17:13:24 发布