parent
be3339fd25
commit
1b0eb5aa96
7 changed files with 493 additions and 2 deletions
@ -0,0 +1,202 @@ |
||||
package com.daqing.financial.hrauth.util; |
||||
|
||||
import java.io.BufferedReader; |
||||
import java.io.InputStream; |
||||
import java.io.InputStreamReader; |
||||
import java.io.OutputStream; |
||||
import java.io.UnsupportedEncodingException; |
||||
import java.net.ConnectException; |
||||
import java.net.HttpURLConnection; |
||||
import java.net.URL; |
||||
|
||||
import javax.net.ssl.HttpsURLConnection; |
||||
import javax.net.ssl.SSLContext; |
||||
import javax.net.ssl.SSLSocketFactory; |
||||
import javax.net.ssl.TrustManager; |
||||
|
||||
import net.sf.json.JSONObject; |
||||
|
||||
public class CommonUtil { |
||||
|
||||
public static JSONObject httpsRequest(String requestUrl, String requestMethod, String outputStr) { |
||||
|
||||
JSONObject jsonObject = null; |
||||
StringBuffer buffer = new StringBuffer(); |
||||
try { |
||||
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
|
||||
TrustManager[] tm = { new MyX509TrustManager() }; |
||||
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE"); |
||||
sslContext.init(null, tm, new java.security.SecureRandom()); |
||||
// 从上述SSLContext对象中得到SSLSocketFactory对象
|
||||
SSLSocketFactory ssf = sslContext.getSocketFactory(); |
||||
|
||||
URL url = new URL(requestUrl); |
||||
HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection(); |
||||
httpUrlConn.setSSLSocketFactory(ssf); |
||||
|
||||
httpUrlConn.setDoOutput(true); |
||||
httpUrlConn.setDoInput(true); |
||||
httpUrlConn.setUseCaches(false); |
||||
// 设置请求方式(GET/POST)
|
||||
httpUrlConn.setRequestMethod(requestMethod); |
||||
|
||||
if ("GET".equalsIgnoreCase(requestMethod)) { |
||||
httpUrlConn.connect(); |
||||
} |
||||
|
||||
|
||||
// 当有数据需要提交时
|
||||
if (null != outputStr) { |
||||
OutputStream outputStream = httpUrlConn.getOutputStream(); |
||||
// 注意编码格式,防止中文乱码
|
||||
outputStream.write(outputStr.getBytes("UTF-8")); |
||||
outputStream.close(); |
||||
} |
||||
|
||||
// 将返回的输入流转换成字符串
|
||||
InputStream inputStream = httpUrlConn.getInputStream(); |
||||
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8"); |
||||
BufferedReader bufferedReader = new BufferedReader(inputStreamReader); |
||||
|
||||
String str = null; |
||||
while ((str = bufferedReader.readLine()) != null) { |
||||
buffer.append(str); |
||||
} |
||||
bufferedReader.close(); |
||||
inputStreamReader.close(); |
||||
// 释放资源
|
||||
inputStream.close(); |
||||
inputStream = null; |
||||
httpUrlConn.disconnect(); |
||||
jsonObject = JSONObject.fromObject(buffer.toString()); |
||||
} catch (ConnectException ce) { |
||||
ce.printStackTrace(); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return jsonObject; |
||||
} |
||||
|
||||
public static String httpRequest(String requestUrl, String requestMethod, String outputStr) { |
||||
|
||||
|
||||
StringBuffer buffer = new StringBuffer(); |
||||
try { |
||||
|
||||
|
||||
URL url = new URL(requestUrl); |
||||
HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection(); |
||||
|
||||
|
||||
httpUrlConn.setDoOutput(true); |
||||
httpUrlConn.setDoInput(true); |
||||
httpUrlConn.setUseCaches(false); |
||||
// 设置请求方式(GET/POST)
|
||||
httpUrlConn.setRequestMethod(requestMethod); |
||||
|
||||
if ("GET".equalsIgnoreCase(requestMethod)) { |
||||
httpUrlConn.connect(); |
||||
} |
||||
|
||||
|
||||
// 当有数据需要提交时
|
||||
if (null != outputStr) { |
||||
OutputStream outputStream = httpUrlConn.getOutputStream(); |
||||
// 注意编码格式,防止中文乱码
|
||||
outputStream.write(outputStr.getBytes("UTF-8")); |
||||
outputStream.close(); |
||||
} |
||||
|
||||
// 将返回的输入流转换成字符串
|
||||
InputStream inputStream = httpUrlConn.getInputStream(); |
||||
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8"); |
||||
BufferedReader bufferedReader = new BufferedReader(inputStreamReader); |
||||
|
||||
String str = null; |
||||
while ((str = bufferedReader.readLine()) != null) { |
||||
buffer.append(str); |
||||
} |
||||
bufferedReader.close(); |
||||
inputStreamReader.close(); |
||||
// 释放资源
|
||||
inputStream.close(); |
||||
inputStream = null; |
||||
httpUrlConn.disconnect(); |
||||
//jsonObject = JSONObject.fromObject(buffer.toString());
|
||||
} catch (ConnectException ce) { |
||||
ce.printStackTrace(); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return buffer.toString(); |
||||
} |
||||
public static String urlEncodeUTF8(String source){ |
||||
String result = source; |
||||
try { |
||||
result = java.net.URLEncoder.encode(source,"utf-8"); |
||||
} catch (UnsupportedEncodingException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
public static String httpsRequestForStr(String requestUrl, String requestMethod, String outputStr) { |
||||
|
||||
String result=""; |
||||
StringBuffer buffer = new StringBuffer(); |
||||
try { |
||||
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
|
||||
TrustManager[] tm = { new MyX509TrustManager() }; |
||||
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE"); |
||||
sslContext.init(null, tm, new java.security.SecureRandom()); |
||||
// 从上述SSLContext对象中得到SSLSocketFactory对象
|
||||
SSLSocketFactory ssf = sslContext.getSocketFactory(); |
||||
|
||||
URL url = new URL(requestUrl); |
||||
HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection(); |
||||
httpUrlConn.setSSLSocketFactory(ssf); |
||||
|
||||
httpUrlConn.setDoOutput(true); |
||||
httpUrlConn.setDoInput(true); |
||||
httpUrlConn.setUseCaches(false); |
||||
// 设置请求方式(GET/POST)
|
||||
httpUrlConn.setRequestMethod(requestMethod); |
||||
|
||||
if ("GET".equalsIgnoreCase(requestMethod)) { |
||||
httpUrlConn.connect(); |
||||
} |
||||
|
||||
|
||||
// 当有数据需要提交时
|
||||
if (null != outputStr) { |
||||
OutputStream outputStream = httpUrlConn.getOutputStream(); |
||||
// 注意编码格式,防止中文乱码
|
||||
outputStream.write(outputStr.getBytes("UTF-8")); |
||||
outputStream.close(); |
||||
} |
||||
|
||||
// 将返回的输入流转换成字符串
|
||||
InputStream inputStream = httpUrlConn.getInputStream(); |
||||
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8"); |
||||
BufferedReader bufferedReader = new BufferedReader(inputStreamReader); |
||||
|
||||
String str = null; |
||||
while ((str = bufferedReader.readLine()) != null) { |
||||
buffer.append(str); |
||||
} |
||||
bufferedReader.close(); |
||||
inputStreamReader.close(); |
||||
// 释放资源
|
||||
inputStream.close(); |
||||
inputStream = null; |
||||
httpUrlConn.disconnect(); |
||||
result=buffer.toString(); |
||||
} catch (ConnectException ce) { |
||||
ce.printStackTrace(); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,108 @@ |
||||
package com.daqing.financial.hrauth.util; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import org.apache.http.Consts; |
||||
import org.apache.http.HttpEntity; |
||||
import org.apache.http.NameValuePair; |
||||
import org.apache.http.client.entity.UrlEncodedFormEntity; |
||||
import org.apache.http.client.methods.CloseableHttpResponse; |
||||
import org.apache.http.client.methods.HttpGet; |
||||
import org.apache.http.client.methods.HttpPost; |
||||
import org.apache.http.impl.client.CloseableHttpClient; |
||||
import org.apache.http.impl.client.HttpClients; |
||||
import org.apache.http.message.BasicNameValuePair; |
||||
import org.apache.http.util.EntityUtils; |
||||
|
||||
public class HttpUtil { |
||||
|
||||
private static final CloseableHttpClient httpclient = HttpClients.createDefault(); |
||||
|
||||
/** |
||||
* 发送HttpGet请求 |
||||
* @param url |
||||
* @return |
||||
*/ |
||||
public static String sendGet(String url) { |
||||
|
||||
HttpGet httpget = new HttpGet(url); |
||||
CloseableHttpResponse response = null; |
||||
try { |
||||
response = httpclient.execute(httpget); |
||||
} catch (IOException e1) { |
||||
e1.printStackTrace(); |
||||
} |
||||
String result = null; |
||||
try { |
||||
HttpEntity entity = response.getEntity(); |
||||
if (entity != null) { |
||||
result = EntityUtils.toString(entity); |
||||
} |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} finally { |
||||
try { |
||||
response.close(); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
/** |
||||
* 发送HttpPost请求,参数为map |
||||
* @param url |
||||
* @param map |
||||
* @return |
||||
*/ |
||||
public static String sendPost(String url, Map<String, String> map) { |
||||
List<NameValuePair> formparams = new ArrayList<NameValuePair>(); |
||||
for (Map.Entry<String, String> entry : map.entrySet()) { |
||||
formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); |
||||
} |
||||
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8); |
||||
HttpPost httppost = new HttpPost(url); |
||||
httppost.setEntity(entity); |
||||
CloseableHttpResponse response = null; |
||||
try { |
||||
response = httpclient.execute(httppost); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
HttpEntity entity1 = response.getEntity(); |
||||
String result = null; |
||||
try { |
||||
result = EntityUtils.toString(entity1); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
/** |
||||
* 发送不带参数的HttpPost请求 |
||||
* @param url |
||||
* @return |
||||
*/ |
||||
public static String sendPost(String url) { |
||||
HttpPost httppost = new HttpPost(url); |
||||
CloseableHttpResponse response = null; |
||||
try { |
||||
response = httpclient.execute(httppost); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
HttpEntity entity = response.getEntity(); |
||||
String result = null; |
||||
try { |
||||
result = EntityUtils.toString(entity); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,26 @@ |
||||
package com.daqing.financial.hrauth.util; |
||||
import java.security.cert.CertificateException; |
||||
import java.security.cert.X509Certificate; |
||||
import javax.net.ssl.X509TrustManager; |
||||
|
||||
/** |
||||
* 信任管理器 |
||||
* |
||||
* @author liufeng |
||||
* @date 2013-04-10 |
||||
*/ |
||||
public class MyX509TrustManager implements X509TrustManager { |
||||
|
||||
// 检查客户端证书
|
||||
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { |
||||
} |
||||
|
||||
// 检查服务器端证书
|
||||
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { |
||||
} |
||||
|
||||
// 返回受信任的X509证书数组
|
||||
public X509Certificate[] getAcceptedIssuers() { |
||||
return null; |
||||
} |
||||
} |
@ -0,0 +1,68 @@ |
||||
package com.daqing.financial.hrauth.util; |
||||
|
||||
import java.util.List; |
||||
|
||||
public class Template { |
||||
|
||||
private String touser; |
||||
private String template_id; |
||||
private String page; |
||||
private List<TemplateParam> templateParamList; |
||||
|
||||
|
||||
public String getTouser() { |
||||
return touser; |
||||
} |
||||
|
||||
public void setTouser(String touser) { |
||||
this.touser = touser; |
||||
} |
||||
|
||||
public String getTemplate_id() { |
||||
return template_id; |
||||
} |
||||
|
||||
public void setTemplate_id(String template_id) { |
||||
this.template_id = template_id; |
||||
} |
||||
|
||||
public String getPage() { |
||||
return page; |
||||
} |
||||
|
||||
public void setPage(String page) { |
||||
this.page = page; |
||||
} |
||||
|
||||
|
||||
|
||||
public String toJSON() { |
||||
StringBuffer buffer = new StringBuffer(); |
||||
buffer.append("{"); |
||||
buffer.append(String.format("\"touser\":\"%s\"", this.touser)).append(","); |
||||
buffer.append(String.format("\"template_id\":\"%s\"", this.template_id)).append(","); |
||||
buffer.append(String.format("\"page\":\"%s\"", this.page)).append(","); |
||||
buffer.append("\"data\":{"); |
||||
TemplateParam param = null; |
||||
for (int i = 0; i < this.templateParamList.size(); i++) { |
||||
param = templateParamList.get(i); |
||||
// 判断是否追加逗号
|
||||
if (i < this.templateParamList.size() - 1){ |
||||
buffer.append(String.format("\"%s\": {\"value\":\"%s\"},", param.getKey(), param.getValue())); |
||||
}else{ |
||||
buffer.append(String.format("\"%s\": {\"value\":\"%s\"}", param.getKey(), param.getValue())); |
||||
} |
||||
} |
||||
buffer.append("}"); |
||||
buffer.append("}"); |
||||
return buffer.toString(); |
||||
} |
||||
|
||||
public List<TemplateParam> getTemplateParamList() { |
||||
return templateParamList; |
||||
} |
||||
|
||||
public void setTemplateParamList(List<TemplateParam> templateParamList) { |
||||
this.templateParamList = templateParamList; |
||||
} |
||||
} |
@ -0,0 +1,25 @@ |
||||
package com.daqing.financial.hrauth.util; |
||||
|
||||
public class TemplateParam { |
||||
|
||||
private String key; |
||||
private String value; |
||||
|
||||
public TemplateParam(String key,String value){ |
||||
this.key=key; |
||||
this.value=value; |
||||
} |
||||
public String getValue() { |
||||
return value; |
||||
} |
||||
public void setValue(String value) { |
||||
this.value = value; |
||||
} |
||||
public String getKey() { |
||||
return key; |
||||
} |
||||
public void setKey(String key) { |
||||
this.key = key; |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue