标签:
//requestUrl 请求的地址
//requestMethod 请求的方法 get post
//outputStr 需要上传到微信服务器上的数据 json 格式
public static String httpRequest(String requestUrl, String requestMethod,
String outputStr) {
String jsonStr = "";
StringBuffer buffer = new StringBuffer();
try {
SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null, new TrustManager[] { myX509TrustManager },
null);
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(true);
// 是不是需要判断method的请求格式取决于自己的业务逻辑
//if ("POST".equals(requestMethod)) {
httpUrlConn.setRequestMethod(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();
jsonStr = buffer.toString();
} catch (ConnectException ce) {
ce.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return jsonStr;
}
private static TrustManager myX509TrustManager = new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
};
微信相关 -- 可以通过调用下面的方法 请求对应的url 获取微信服务器返回的json数据
标签:
原文地址:http://www.cnblogs.com/yexiaocheng/p/5063580.html