ad5081d3
孙向锦
初始化项目
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package com.fh.util.weixin;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import net.sf.json.JSONObject;
|
ad5081d3
孙向锦
初始化项目
|
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
public class Weixin {
public final static String access_token_url = "https://api.weixin.qq.com/cgi-bin/token?" + "grant_type=client_credential&appid=APPID&secret=APPSECRET";
public final static String gz_url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=";
/**
* 获取access_token
*
* @param appid
* @param appsecret
* @return
*/
public String getAccess_token(String appid, String appsecret) {
try {
String requestUrl = access_token_url.replace("APPID", appid).replace("APPSECRET", appsecret);
JSONObject jsonObject = httpRequst(requestUrl, "GET", null);
return jsonObject.getString("access_token");
} catch (Exception e) {
return "errer";
}
}
/**
* 获取关注列表
*
* @param appid
* @param appsecret
*/
public void getGz(String appid, String appsecret) {
try {
String access_token = getAccess_token(appid, appsecret);
System.out.println(access_token + "============");
String requestUrl = gz_url.replace("ACCESS_TOKEN", access_token);
System.out.println(requestUrl + "============");
JSONObject jsonObject = httpRequst(requestUrl, "GET", null);
System.out.println(jsonObject);
// System.out.println(jsonObject.getString("total")+"============");
/*
* PrintWriter pw; try { pw = new PrintWriter( new FileWriter(
* "e:/gz.txt" ) ); pw.print(jsonObject.getString("total"));
* pw.close(); } catch (IOException e) { // TODO Auto-generated
* catch block e.printStackTrace(); }
*
* out.write("success"); out.close();
*/
} catch (Exception e) {
}
}
public JSONObject httpRequst(String requestUrl, String requetMethod,
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对象中得到SSLSocktFactory对象
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(requetMethod);
if ("GET".equalsIgnoreCase(requetMethod))
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) {
// TODO: handle exception
} catch (Exception e) {
}
return jsonobject;
}
}
class MyX509TrustManager implements X509TrustManager {
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
// TODO Auto-generated method stub
}
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
// TODO Auto-generated method stub
}
public X509Certificate[] getAcceptedIssuers() {
// TODO Auto-generated method stub
return null;
}
}
|