package commons;
import commons.weibo4j.util.URLEncodeUtils;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
* 网络请求工具
*
* @author Haner
*/
public class HttpUtil {
private static HttpClient httpClient = null;
private static HttpGet httpGet;
private static HttpPost httpPost;
public static String TIME_OUT = "请求超时";
public static String SERVER_ERROR = "服务器异常";
public static String NETWORK_ERROR = "网络异常";
static {
httpClient = getHttpClient();
}
/**
* Http Get请求 请求地址
*
* @param url Get参数
* @param params 编码
* @param encode 返回请求结果
* @return
* @throws org.apache.http.client.ClientProtocolException
* @throws java.io.IOException
*/
public static String sendGetRequest(String url, Map<String, String> params,
String encode) {
String result = null;
try {
if (null == params) {
httpGet = new HttpGet(url);
} else {
httpGet = new HttpGet(url + dealGetParams(params, encode));
}
HttpResponse response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
result = EntityUtils.toString(response.getEntity());
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result != null ? result : "";
}
public static String sendGetRequest(String url, Map<String, String> params)
throws ClientProtocolException, IOException {
return sendGetRequest(url, params, "UTF-8");
}
public static String sendGetRequest(String url)
throws ClientProtocolException, IOException {
return sendGetRequest(url, null, "UTF-8");
}
public static String sendGetRequestGB2312(String url)
throws ClientProtocolException, IOException {
return sendGetRequest(url, null, "GB2312");
}
/**
* POST请求 返回请求结果 HashMap键值参数
*
* @param params
* @return
* @throws Exception
*/
@SuppressWarnings({"deprecation", "unchecked"})
public static String sendPostRequest(String url, Object params, String encode) throws Exception {
String resultStr = null;
httpPost = new HttpPost(url);
if (params != null) {
StringEntity entity;
if (params instanceof Map) {
entity = new StringEntity(dealPostParams(
(HashMap<String, String>) params, encode));
} else if (params instanceof String) {
entity = new StringEntity((String) params, encode);
} else if (params instanceof List) {
entity = new UrlEncodedFormEntity(
(List<? extends NameValuePair>) params, encode);
} else {
throw new Exception("参数有误!");
}
// httpPost.setHeader("Authorization","Bearer access_token");
httpPost.setEntity(entity);
}
try {
HttpResponse response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
resultStr = EntityUtils.toString(response.getEntity());
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return resultStr != null ? resultStr : null;
}
public static String sendPostRequest(String url) throws Exception {
return sendPostRequest(url,"");
}
/**
* 键值对请求 默认UTF-8编码
*
* @param url
* @param params
* @return
* @throws Exception
*/
public static String sendPostRequest(String url, Map<String, String> params)
throws Exception {
return sendPostRequest(url, params, "UTF-8");
}
/**
* String 默认UTF-8编码
*
* @param url
* @param params
* @return
* @throws Exception
*/
public static String sendPostRequest(String url, String params)
throws Exception {
return sendPostRequest(url, params==null?null:params, "UTF-8");
}
/**
* 键值对请求 默认UTF-8编码
*
* @param url
* @param params
* @return
* @throws Exception
*/
public static String sendPostRequest(String url, List<NameValuePair> params)
throws Exception {
return sendPostRequest(url, params, "UTF-8");
}
/**
* 处理Get方式请求的URL
*
* @param params
* @param enc
* @return
* @throws java.io.UnsupportedEncodingException
*/
private static String dealGetParams(Map<String, String> params, String enc)
throws UnsupportedEncodingException {
StringBuffer sb = new StringBuffer();
sb.append("?");
for (Entry<String, String> entry : params.entrySet()) {
// 参数名=参数&参数名=参数
sb.append(entry.getKey()).append("=")
.append(URLEncoder.encode(entry.getValue(), enc))
.append("&");
}
// 删除最后一个&
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
/**
* 处理POST请求URL
*
* @param params
* @param enc
* @return
*/
private static String dealPostParams(Map<String, String> params, String enc) {
StringBuffer sb = new StringBuffer();
for (Entry<String, String> entry : params.entrySet()) {
try {
sb.append(entry.getKey()).append("=")
.append(URLEncoder.encode(entry.getValue(), enc))
.append("&");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
/**
* 获取HttpClient
*
* @return
*/
@SuppressWarnings("deprecation")
public static synchronized HttpClient getHttpClient() {
if (null == httpClient) {
httpClient = new DefaultHttpClient();
}
return httpClient;
}
}
- 等 最代码怎么获取牛币啊?
- 完 谁来告诉我最代码上线的时间,答对者给5牛币,先来先得
- 等 牛友们,大家好,你们做程序员多久了?现在还好吗?
- 完 在微信打开的页面里进行app下载
- 等 最代码2014年欢乐聚声会
- 完 mysql如何查询表数据并且对3个字段降序的SQL?
- 完 最代码牛币机制改革
- 完 成功的在bae上使用了自定义运行环境 jetty+nginx的组合,大家对jetty+nginx优化有哪些心得?
- 完 进来分享一下各位牛牛是如何加入最代码大家庭的?
- 等 为什么java BufferedImage类处理大图直接抛出内存溢出的异常?
- 等 最代码是否开发手机app客户端?
- 完 java程序员学习哪些java的技术?java有哪些框架?都能做哪方面的开发?
- 等 php格式网页文件怎么运行?
- 等 Java volatile值获取的问题
- 等 前端vue,拦截了登录后台后,返回的token,requests拦截token,但是发送请求的时候,就出现跨越异常
- 等 大专本科计算机科班怎么找到Java工作?
- 等 eclipse怎么把三个java swing游戏项目合成一个项目?
- 完 伙伴们,大家都有什么好的解压方式么,分享一下~
- 完 三四线城市,6、7k,运维工作,索然无味,想去辞职上培训,各位牛牛有什么建议嘛
- 等 jsp页面输入中文变成问号
- 等 JPA在线上运行一段时间后报错Caused by: java.lang.IncompatibleClassChangeError: null
- 等 PHP 这个规则用preg_match_all怎么写
- 等 大佬们,有没有知道Alfresco如何配置LDAP登录呢?
- 等 php的install目录是框架带的吗?
- 等 webservice问题求解决,急急急
- 完 可否有人简单口述一下什么是webService接口?
- 完 weblogic服务器中调用其他系统中的webservice接口,报权限问题
- 等 如何在html静态网页调用axis2 webservice接口?
- 完 如何在java Web项目中开发WebService接口
- 完 java webservice如何连接访问数据库
- 完 Java如何发布WebService接口供客户端调用
- 完 java webservice参数与返回值不固定的方法如何发布?
- 完 Android开发中调用Spring CXF整合发布的java WebService接口为什么抛出异常错误?
- 等 java调用webservice接口出错怎么解决?
- 完 java调用WebService接口为什么这么慢?和数据库或发布WebService的方式有关系吗?
- 完 手持仪WebService异常信息记录数据库是否合理?