package weixin;
import java.util.Date;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import winxin.pojo.Token;
import com.zhiwan.ballInterface.HttpBallData;
/**
* https://mp.weixin.qq.com/
* mark
*
* @author ZengWenFeng
*/
public class WeiXinUtil
{
private static Logger log = LoggerFactory.getLogger(WeiXinUtil.class);
private static long tokenTime = 0;
//------------------------------------------------------------------------------------------------------------------------------------------------
//微信公众平台接口调试工具
//https://mp.weixin.qq.com/debug
//------------------------------------------------------------------------------------------------------------------------------------------------
//一、接口类型: 基础支持
//------------------------------------------------------------------------------------------------------------------------------------------------
//二、接口列表:获取access_token接口/token 方法:GET
//三、参数列表:
// grant_type 必须 client_credential
// appid 必须 填写您的AppID
// secret 必须 填写您的AppSecret
public final static String url_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
//获取微信服务器IP地址
public final static String url_get_weixin_server_ip = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=ACCESS_TOKEN";
//二、接口列表:多媒体文件上传接口/media/upload 方法:POST
//三、参数列表:
// access_token 必须 请求url_token获取
// type 必须 媒体文件类型,分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb)
// media 必须 选择文件 form-data中媒体文件标识,有filename、filelength、content-type等信息
public static String url_upload_media_image = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=image";
public static String url_upload_media_voice = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=voice";
public static String url_upload_media_video = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=video";
public static String url_upload_media_thumb = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=thumb";
//四、返回
/*
{
"type": "image",
"media_id": "1q5oAg0ysZjjfMnkQLywndf4nGRWwJp_pz2Mp1O9hamZUMON_7b_wkVh4YQPPqLU",
"created_at": 1499512267
}
*/
public final static String url_get_user = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID";
private static Token token = null;
public WeiXinUtil()
{
}
public static Token getToken(String appid, String appsecret)
{
long now = new Date().getTime();
//token有效时间 7e6 毫秒
if (tokenTime != 0 && now - tokenTime < 7000000)
{
return token;
}
String requestUrl = url_token.replace("APPID", appid).replace("APPSECRET", appsecret);
// 发起GET请求获取凭证
JSONObject jsonObject = HttpBallData.httpGet(requestUrl);
if (null != jsonObject)
{
try
{
token = new Token();
token.setAccessToken(jsonObject.getString("access_token"));
token.setExpiresIn(jsonObject.getInt("expires_in"));
tokenTime = now;
}
catch (JSONException e)
{
token = null;
// 获取token失败
log.error("获取token失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
}
}
return token;
}
/**
* 获取微信服务器IP地址
*
* @author ZengWenFeng
* @return 返回JSON
*
{
"ip_list":
[
"101.226.62.77","101.226.62.78","101.226.62.79","101.226.62.80","101.226.62.81",
"101.226.62.82","101.226.62.83","101.226.62.84","101.226.62.85","101.226.62.86",
"101.226.103.59","101.226.103.60","101.226.103.61","101.226.103.62","101.226.103.63",
"101.226.103.69","101.226.103.70","101.226.103.71","101.226.103.72","101.226.103.73",
"140.207.54.73","140.207.54.74","140.207.54.75","140.207.54.76","140.207.54.77",
"140.207.54.78","140.207.54.79","140.207.54.80",
"182.254.11.203","182.254.11.202","182.254.11.201","182.254.11.200","182.254.11.199",
"182.254.11.198",
"59.37.97.100","59.37.97.101","59.37.97.102","59.37.97.103","59.37.97.104",
"59.37.97.105","59.37.97.106","59.37.97.107","59.37.97.108","59.37.97.109",
"59.37.97.110","59.37.97.111","59.37.97.112","59.37.97.113","59.37.97.114",
"59.37.97.115","59.37.97.116","59.37.97.117","59.37.97.118",
"112.90.78.158","112.90.78.159","112.90.78.160","112.90.78.161","112.90.78.162",
"112.90.78.163","112.90.78.164","112.90.78.165","112.90.78.166","112.90.78.167",
"140.207.54.19","140.207.54.76","140.207.54.77","140.207.54.78","140.207.54.79","140.207.54.80",
"180.163.15.149","180.163.15.151","180.163.15.152","180.163.15.153","180.163.15.154",
"180.163.15.155","180.163.15.156","180.163.15.157","180.163.15.158","180.163.15.159",
"180.163.15.160","180.163.15.161","180.163.15.162","180.163.15.163","180.163.15.164",
"180.163.15.165","180.163.15.166","180.163.15.167","180.163.15.168","180.163.15.169","180.163.15.170",
"101.226.103.0/25","101.226.233.128/25","58.247.206.128/25","182.254.86.128/25","103.7.30.21",
"103.7.30.64/26","58.251.80.32/27","183.3.234.32/27","121.51.130.64/27"
]
}
*/
public static JSONObject getWeixinServerIp(String accessToken)
{
String requestUrl = url_get_weixin_server_ip.replace("ACCESS_TOKEN", accessToken);
JSONObject jsonObject = HttpBallData.httpGet(requestUrl);
return jsonObject;
}
public static Object[] getWeixinServerIp2(String accessToken)
{
String requestUrl = url_get_weixin_server_ip.replace("ACCESS_TOKEN", accessToken);
JSONObject jsonObject = HttpBallData.httpGet(requestUrl);
JSONArray jsonArray = jsonObject.getJSONArray("ip_list");
return jsonArray.toArray();
}
public static void main(String[] args)
{
//公众号:gh_867ea0abdfe2
String AppID = "ZENGWENFENG56b";//u guess
String AppSecret = "ZENGWENFENG15168b38745";//u guess
Token t = getToken(AppID, AppSecret);
String accessToken = t.getAccessToken();
System.out.println(t.getAccessToken());
System.out.println(t.getExpiresIn());
// String requestUrl = url_get_user.replace("ACCESS_TOKEN", accessToken).replace("NEXT_OPENID", "");
// JSONObject jsonObject = HttpBallData.httpGet(requestUrl);
// System.out.println(jsonObject);
// JSONObject jsonObject1 = getWeixinServerIp(accessToken);
// System.out.println(jsonObject1);
Object[] arrObj = getWeixinServerIp2(accessToken);
for (int i = 0; i < arrObj.length; i++)
{
System.out.println(arrObj[i]);
}
}
}
package com.zhiwan.ballInterface;
import java.io.IOException;
import java.net.URLDecoder;
import net.sf.json.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@SuppressWarnings("deprecation")
public class HttpBallData
{
private static Logger logger = LoggerFactory.getLogger(HttpBallData.class); //日志记录
/**
* httpPost
*
* @param url 路径
* @param jsonParam 参数
* @return
*/
public static JSONObject httpPost(String url, JSONObject jsonParam)
{
return httpPost(url, jsonParam, false);
}
/**
* post请求
*
* @param url url地址
* @param jsonParam 参数
* @param noNeedResponse 不需要返回结果
* @return
*/
public static JSONObject httpPost(String url, JSONObject jsonParam, boolean noNeedResponse)
{
//post请求返回结果
DefaultHttpClient httpClient = new DefaultHttpClient();
// HttpClient httpClient = new HttpClient();
JSONObject jsonResult = null;
HttpPost method = new HttpPost(url);
try
{
if (null != jsonParam)
{
//解决中文乱码问题
StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
method.setEntity(entity);
}
HttpResponse result = httpClient.execute(method);
url = URLDecoder.decode(url, "UTF-8");
/** 请求发送成功,并得到响应 **/
if (result.getStatusLine().getStatusCode() == 200)
{
String str = "";
try
{
/** 读取服务器返回过来的json字符串数据 **/
str = EntityUtils.toString(result.getEntity());
if (noNeedResponse)
{
return null;
}
/** 把json字符串转换成json对象 **/
jsonResult = JSONObject.fromObject(str);
}
catch (Exception e)
{
logger.error("post请求提交失败:" + url, e);
}
}
}
catch (IOException e)
{
logger.error("post请求提交失败:" + url, e);
}
return jsonResult;
}
/**
* 发送get请求
*
* @param url 路径
* @return
*/
public static JSONObject httpGet(String url)
{
//get请求返回结果
JSONObject jsonResult = null;
try
{
DefaultHttpClient client = new DefaultHttpClient();
//发送get请求
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
/** 请求发送成功,并得到响应 **/
if (response.getStatusLine().getStatusCode() == org.apache.http.HttpStatus.SC_OK)
{
/** 读取服务器返回过来的json字符串数据 **/
String strResult = EntityUtils.toString(response.getEntity());
/** 把json字符串转换成json对象 **/
jsonResult = JSONObject.fromObject(strResult);
url = URLDecoder.decode(url, "UTF-8");
}
else
{
logger.error("get请求提交失败:" + url);
}
}
catch (IOException e)
{
logger.error("get请求提交失败:" + url, e);
}
return jsonResult;
}
/**
* 测试球类数据接口
*/
public static void main(String[] args)
{
String url = "http://feed.sportsdt.com/basketball/testing.aspx?type=getlivegame&from=kuaiqiu";
//JSONObject result = httpGet(url);
//System.out.println(result);
url="http://feed.sportsdt.com/basketball/testing.aspx";
JSONObject jsonParam=new JSONObject();
jsonParam.put("type", "getlivegame");
jsonParam.put("from", "kuaiqiu");
JSONObject result=httpPost(url, jsonParam);
System.out.println(result);
/*JSONArray schedule = result.getJSONArray("Schedule");
System.out.println(schedule);
JSONObject competition = result.getJSONObject("Competition");
System.out.println(competition);
JSONObject team = result.getJSONObject("Team");
System.out.println(team);*/
}
}