鄙人开发过程中遇到这样一个问题,一台主机,未加微信后台白名单,上面部署了两个程序为A、B,现在A是通过jeecg开发,B为普通开发,问题是,A可以获取到OpenId,B会提示{"errcode":40164,"errmsg":"invalid ip xxxxxxxxxx ipv6 ::ffff:xxxxxxxxxxx, not in whitelist hint: [YGs5FA08331533]"}。为什么会出现这样的情况
A发起的请求的代码是
public static JSONObject httpRequest(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());
//jsonObject = JSONObject.fromObject(buffer.toString());
} catch (ConnectException ce) {
org.jeecgframework.core.util.LogUtil.info("Weixin server connection timed out.");
} catch (Exception e) {
org.jeecgframework.core.util.LogUtil.info("https request error:{}" + e.getMessage());
}
return jsonObject;
}
B发起请求的代码是
public static String sendGet(String url, String param) {
log.info("GET请求:\nurl:{}\nparam:{}", url, param);
String result = "";
BufferedReader in = null;
/**
* 创建一个计时器
*/
StopWatch watch = new StopWatch();
/**
* 计时器开始
*/
watch.start();
try {
String urlNameString = url;
if (!StringUtils.isEmpty(param)) {
urlNameString += "?" + param;
}
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
connection.setConnectTimeout(connectTimeOut);
// 缓存的最长时间
connection.setReadTimeout(readTimeOut);
// 允许输入
connection.setDoInput(true);
// 允许输出
connection.setDoOutput(true);
// 不允许使用缓存
connection.setUseCaches(false);
connection.setRequestMethod("GET");
// 设置通用的请求属性
connection.setRequestProperty("Accept", "*/*");
connection.setRequestProperty("Content-type", "application/json;charset=UTF-8");
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
log.info(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (BusinessException e) {
log.error("发送 GET 请求出现异常!" + e);
e.printStackTrace();
throw new BusinessException(e.getMessage());
} catch (Exception e) {
log.error("发送 GET 请求出现异常!" + e);
e.printStackTrace();
throw new RuntimeException("GET请求超时.");
} finally {// 使用finally块来关闭输入流
watch.stop();
log.info("请求耗时[{}]ms", watch.getTime());
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
log.error("GET 关闭输入流出现异常!" + e2.getMessage());
e2.printStackTrace();
throw new RuntimeException("GET请求超时.");
}
}
if (StringUtils.isEmpty(result)) {
throw new BusinessException("请求超时.");
}
log.info("请求返回:" + result);
return result;
}