com.jfinal.kit.HttpKit Java Examples

The following examples show how to use com.jfinal.kit.HttpKit. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AccessTokenApi.java    From jfinal-weixin with Apache License 2.0 6 votes vote down vote up
/**
 * 强制更新 access token 值
 */
public static synchronized void refreshAccessToken() {
	ApiConfig ac = ApiConfigKit.getApiConfig();
	AccessToken result = null;
	for (int i=0; i<3; i++) {	// 最多三次请求
		String appId = ac.getAppId();
		String appSecret = ac.getAppSecret();
		Map<String, String> queryParas = ParaMap.create("appid", appId).put("secret", appSecret).getData();
		String json = HttpKit.get(url, queryParas);
		result = new AccessToken(json);
		
		if (result.isAvailable())
			break;
	}
	
	// 三次请求如果仍然返回了不可用的 access token 仍然 put 进去,便于上层通过 AccessToken 中的属性判断底层的情况
	accessTokenCache.set(ac.getAppId(), result);
}
 
Example #2
Source File: MediaApi.java    From jfinal-weixin with Apache License 2.0 6 votes vote down vote up
/**
 * 获取素材列表
 * @param mediaType 素材的类型,图片(image)、视频(video)、语音 (voice)、图文(news)
 * @param offset 从全部素材的该偏移位置开始返回,0表示从第一个素材 返回
 * @param count 返回素材的数量,取值在1到20之间
 * @return ApiResult 返回信息
 */
public static ApiResult batchGetMaterial(MediaType mediaType, int offset, int count) {
	String url = batchget_material_url + AccessTokenApi.getAccessTokenStr();
	
	if(offset < 0) offset = 0;
	if(count > 20) count = 20;
	if(count < 1) count = 1;
	
	Map<String, Object> dataMap = new HashMap<String, Object>();
	dataMap.put("type", mediaType.get());
	dataMap.put("offset", offset);
	dataMap.put("count", count);
	
	String jsonResult = HttpKit.post(url, JsonUtils.toJson(dataMap));
	return new ApiResult(jsonResult);
}
 
Example #3
Source File: GroupsApi.java    From jfinal-weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 查询所有分组
 * @return ApiResult
 */
public static ApiResult get() {
	String url = getUrl + AccessTokenApi.getAccessTokenStr();
	
	String jsonResult = HttpKit.get(url);
	return new ApiResult(jsonResult);
}
 
Example #4
Source File: MsgController.java    From jfinal-weixin with Apache License 2.0 5 votes vote down vote up
@Before(NotAction.class)
public String getInMsgXml() {
    if (inMsgXml == null) {
        inMsgXml = HttpKit.readIncommingRequestData(getRequest());

        // 是否需要解密消息
        if (ApiConfigKit.getApiConfig().isEncryptMessage()) {
            inMsgXml = MsgEncryptKit.decrypt(inMsgXml, getPara("timestamp"), getPara("nonce"), getPara("msg_signature"));
        }
    }
    return inMsgXml;
}
 
Example #5
Source File: SnsAccessTokenApi.java    From jfinal-weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 通过code获取access_token
 *
 * @param code   第一步获取的code参数
 * @param appId  应用唯一标识
 * @param secret 应用密钥AppSecret
 * @return SnsAccessToken
 */
public static SnsAccessToken getSnsAccessToken(String appId, String secret, String code)
{
    SnsAccessToken result = null;
    for (int i = 0; i < 3; i++)
    {    // 最多三次请求
        Map<String, String> queryParas = ParaMap.create("appid", appId).put("secret", secret).put("code", code).getData();
        String json = HttpKit.get(url, queryParas);
        result = new SnsAccessToken(json);

        if (result.isAvailable())
            break;
    }
    return result;
}
 
Example #6
Source File: DatacubeApi.java    From jfinal-weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 获取统计信息
 * @param url url链接
 * @param begin_date 获取数据的起始日期,begin_date和end_date的差值需小于“最大时间跨度”(比如最大时间跨度为1时,begin_date和end_date的差值只能为0,才能小于1),否则会报错
 * @param end_date 获取数据的结束日期,end_date允许设置的最大值为昨日
 * @return ApiResult
 */
private static ApiResult getData(String url, String begin_date, String end_date) {
	Map<String, String> mapData = new HashMap<String, String>();
	mapData.put("begin_date", begin_date);
	mapData.put("end_date", end_date);
	
	String jsonResult = HttpKit.post(url, JsonUtils.toJson(mapData));
	return new ApiResult(jsonResult);
}
 
Example #7
Source File: UserApi.java    From jfinal-weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 设置备注名
 * @param openid 用户标识
 * @param remark 新的备注名,长度必须小于30字符
 * @return
 */
public static ApiResult updateRemark(String openid, String remark) {
	String url = updateRemarkUrl + AccessTokenApi.getAccessTokenStr();
	
	Map<String, String> mapData = new HashMap<String, String>();
	mapData.put("openid", openid);
	mapData.put("remark", remark);
	String jsonResult = HttpKit.post(url, JsonUtils.toJson(mapData));
	
	return new ApiResult(jsonResult);
}
 
Example #8
Source File: UserApi.java    From jfinal-weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 获取用户列表
 * @param nextOpenid 第一个拉取的OPENID,不填默认从头开始拉取
 * @return ApiResult
 */
public static ApiResult getFollowers(String nextOpenid) {
	ParaMap pm = ParaMap.create("access_token", AccessTokenApi.getAccessTokenStr());
	if (nextOpenid != null)
		pm.put("next_openid", nextOpenid);
	return new ApiResult(HttpKit.get(getFollowers, pm.getData()));
}
 
Example #9
Source File: PaymentApi.java    From jfinal-weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 支付相关请求
 */
private static Map<String, String> request(String url, Map<String, String> params, String paternerKey) {
	params.put("nonce_str", PaymentKit.getUUID());
	String sign = PaymentKit.createSign(params, paternerKey);
	params.put("sign", sign);
	String xmlStr = HttpKit.post(url, PaymentKit.toXml(params));
	return PaymentKit.xmlToMap(xmlStr);
}
 
Example #10
Source File: GroupsApi.java    From jfinal-weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 删除分组
 * @param id 分组的id
 * @return ApiResult
 */
public static ApiResult delete(int id) {
	String url = deleteUrl + AccessTokenApi.getAccessTokenStr();
	
	Map<String, Map<String, Object>> groupData = new HashMap<String, Map<String, Object>>();
	Map<String, Object> mapData = new HashMap<String, Object>();
	mapData.put("id", id);
	groupData.put("group", mapData);
	
	String jsonResult = HttpKit.post(url, JsonUtils.toJson(groupData));
	return new ApiResult(jsonResult);
}
 
Example #11
Source File: GroupsApi.java    From jfinal-weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 批量移动用户分组
 * @param openidList 用户唯一标识符openid的列表(size不能超过50)
 * @param to_groupid 分组id
 * @return ApiResult
 */
public static ApiResult membersBatchUpdate(List<String> openidList, int to_groupid) {
	String url = membersBatchUpdateUrl + AccessTokenApi.getAccessTokenStr();
	
	Map<String, Object> mapData = new HashMap<String, Object>();
	mapData.put("openid_list", openidList);
	mapData.put("to_groupid", to_groupid);
	
	String jsonResult = HttpKit.post(url, JsonUtils.toJson(mapData));
	return new ApiResult(jsonResult);
}
 
Example #12
Source File: GroupsApi.java    From jfinal-weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 移动用户分组
 * @param openid 用户唯一标识符
 * @param to_groupid 分组id
 * @return ApiResult
 */
public static ApiResult membersUpdate(String openid, int to_groupid) {
	String url = membersUpdateUrl + AccessTokenApi.getAccessTokenStr();
	
	Map<String, Object> mapData = new HashMap<String, Object>();
	mapData.put("openid", openid);
	mapData.put("to_groupid", to_groupid);
	
	String jsonResult = HttpKit.post(url, JsonUtils.toJson(mapData));
	return new ApiResult(jsonResult);
}
 
Example #13
Source File: GroupsApi.java    From jfinal-weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 修改分组名
 * @param id 分组id,由微信分配
 * @param name 分组名字(30个字符以内)
 * @return ApiResult
 */
public static ApiResult update(int id, String name) {
	String url = updateUrl + AccessTokenApi.getAccessTokenStr();
	
	Map<String, Map<String, Object>> groupData = new HashMap<String, Map<String, Object>>();
	Map<String, Object> mapData = new HashMap<String, Object>();
	mapData.put("id", id);
	mapData.put("name", name);
	groupData.put("group", mapData);
	
	String jsonResult = HttpKit.post(url, JsonUtils.toJson(groupData));
	return new ApiResult(jsonResult);
}
 
Example #14
Source File: GroupsApi.java    From jfinal-weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 通过用户的OpenID查询其所在的GroupID
 * @param openid 普通用户的标识,对当前开发者帐号唯一
 * @return ApiResult
 */
public static ApiResult getId(String openid) {
	String url = getIdUrl + AccessTokenApi.getAccessTokenStr();
	
	Map<String, String> mapData = new HashMap<String, String>();
	mapData.put("openid", openid);
	
	String jsonResult = HttpKit.post(url, JsonUtils.toJson(mapData));
	return new ApiResult(jsonResult);
}
 
Example #15
Source File: GroupsApi.java    From jfinal-weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 创建分组,一个公众账号,最多支持创建100个分组。
 * @param name 分组名
 * @return ApiResult
 */
public static ApiResult create(String name) {
	String url = createUrl + AccessTokenApi.getAccessTokenStr();
	
	Map<String, Map<String, String>> groupData = new HashMap<String, Map<String, String>>();
	Map<String, String> mapData = new HashMap<String, String>();
	mapData.put("name", name);
	groupData.put("group", mapData);
	
	String jsonResult = HttpKit.post(url, JsonUtils.toJson(groupData));
	return new ApiResult(jsonResult);
}
 
Example #16
Source File: HttpExtKit.java    From jfinal-ext3 with Apache License 2.0 5 votes vote down vote up
public static byte[] readByteData(HttpServletRequest request) {
	String ret = HttpKit.readData(request);
	byte[] data = null;
	try {
		data = ret.getBytes("UTF-8");
	} catch (UnsupportedEncodingException e) {
		new RuntimeException(e);
	}
	return data;
}
 
Example #17
Source File: MediaApi.java    From jfinal-weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 获取素材总数
 * @return ApiResult 返回信息
 */
public static ApiResult getMaterialCount() {
	String url = get_materialcount_url + AccessTokenApi.getAccessTokenStr();
	System.out.println(url);
	String jsonResult = HttpKit.get(url);
	return new ApiResult(jsonResult);
}
 
Example #18
Source File: MediaApi.java    From jfinal-weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 修改永久图文素材
 * @param media_id 要修改的图文消息的id
 * @param index 要更新的文章在图文消息中的位置(多图文消息时,此字段才有意义),第一篇为0
 * @param mediaArticles 图文素材
 * @return ApiResult 返回信息
 */
public static ApiResult updateNews(String media_id, int index, MediaArticles mediaArticles) {
	String url = update_news_url + AccessTokenApi.getAccessTokenStr();
	
	Map<String, Object> dataMap = new HashMap<String, Object>();
	dataMap.put("media_id", media_id);
	dataMap.put("index", index);
	dataMap.put("articles", mediaArticles);
	
	String jsonResult = HttpKit.post(url, JsonUtils.toJson(dataMap));
	return new ApiResult(jsonResult);
}
 
Example #19
Source File: MediaApi.java    From jfinal-weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 删除永久素材
 * @param media_id 要获取的素材的media_id
 * @return ApiResult 返回信息
 */
public static ApiResult delMaterial(String media_id) {
	String url = del_material_url + AccessTokenApi.getAccessTokenStr();
	
	Map<String, Object> dataMap = new HashMap<String, Object>();
	dataMap.put("media_id", media_id);
	
	String jsonResult = HttpKit.post(url, JsonUtils.toJson(dataMap));
	return new ApiResult(jsonResult);
}
 
Example #20
Source File: MediaApi.java    From jfinal-weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 新增永久图文素材
 * @param mediaArticles 图文列表
 * @return ApiResult
 */
public static ApiResult addNews(List<MediaArticles> mediaArticles) {
	String url = add_news_url + AccessTokenApi.getAccessTokenStr();
	
	Map<String, Object> dataMap = new HashMap<String, Object>();
	dataMap.put("articles", mediaArticles);
	
	String jsonResult = HttpKit.post(url, JsonUtils.toJson(dataMap));
	return new ApiResult(jsonResult);
}
 
Example #21
Source File: MediaApi.java    From jfinal-weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 视频群发的消息素材上传
 * @param mediaId 用于群发的消息的media_id
 * @param title 消息的标题
 * @param description 消息的描述
 * @return
 */
public static ApiResult uploadVideo(String mediaId, String title, String description) {
	String url = uploadVideoUrl + AccessTokenApi.getAccessTokenStr();
	
	Map<String, String> mapData = new HashMap<String, String>();
	mapData.put("media_id", mediaId);
	mapData.put("title", title);
	mapData.put("description", description);
	
	String jsonResult = HttpKit.post(url, JsonUtils.toJson(mapData));
	return new ApiResult(jsonResult);
}
 
Example #22
Source File: WeixinPayController.java    From jfinal-weixin with Apache License 2.0 5 votes vote down vote up
public void pay_notify() throws DocumentException {
	// 支付结果通用通知文档: https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7
	
	String xmlMsg = HttpKit.readIncommingRequestData(getRequest());
	System.out.println("支付通知="+xmlMsg);
	Map<String, String> params = PaymentKit.xmlToMap(xmlMsg);
	
	String result_code  = params.get("result_code");
	String totalFee     = params.get("total_fee");
	String orderId      = params.get("out_trade_no");
	String transId      = params.get("transaction_id");
	String timeEnd      = params.get("time_end");
	
	if(PaymentKit.verifyNotify(params, paternerKey)){
		if (("SUCCESS").equals(result_code)) {
			//更新订单信息
			System.out.println("更新订单信息");
			
			Map<String, String> xml = new HashMap<String, String>();
			xml.put("return_code", "SUCCESS");
			xml.put("return_msg", "OK");
			renderText(PaymentKit.toXml(xml));
			return;
		}
	}
	renderText("");
}
 
Example #23
Source File: JsTicketApi.java    From jfinal-weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * http GET请求获得jsapi_ticket(有效期7200秒,开发者必须在自己的服务全局缓存jsapi_ticket)
 * 
 * @param jsApiType
 * @return JsTicket
 */
public static JsTicket getTicket(JsApiType jsApiType) {
	String access_token = AccessTokenApi.getAccessTokenStr();
	String appId = ApiConfigKit.getApiConfig().getAppId();
	String key = appId + ':' + jsApiType.name();
	
	JsTicket jsTicket = accessTokenCache.get(key);
	if (null == jsTicket || !jsTicket.isAvailable()) {
		ParaMap pm = ParaMap.create("access_token", access_token).put("type", jsApiType.name());
		jsTicket = new JsTicket(HttpKit.get(apiUrl, pm.getData()));
		accessTokenCache.set(key, jsTicket);
	}
	return jsTicket;
}
 
Example #24
Source File: CustomServiceApi.java    From jfinal-weixin with Apache License 2.0 4 votes vote down vote up
/**
 * 获取客服聊天记录
 */
public static ApiResult getRecord(String jsonStr) {
    String jsonResult = HttpKit.post(getRecordUrl + AccessTokenApi.getAccessTokenStr(), jsonStr);
    return new ApiResult(jsonResult);
}
 
Example #25
Source File: HttpExtKit.java    From jfinal-ext3 with Apache License 2.0 4 votes vote down vote up
/**
 * Send byte data by post request
 */
public static String post(String url, Map<String, String> queryParas, byte[] data, Map<String, String> headers) {
	return HttpKit.post(url, queryParas, (new String(data)), headers);
}
 
Example #26
Source File: HttpExtKit.java    From jfinal-ext3 with Apache License 2.0 4 votes vote down vote up
public static byte[] readHexByteData(HttpServletRequest request) {
	String ret = HttpKit.readData(request);
	return HexKit.HexStringToBytes(ret);
}
 
Example #27
Source File: WechatAuthorizationController.java    From jpress with GNU Lesser General Public License v3.0 4 votes vote down vote up
private static ApiResult getUserInfo(String openId, String accessToken) {
    ParaMap pm = ParaMap.create("access_token", accessToken).put("openid", openId).put("lang", "zh_CN");
    return new ApiResult(HttpKit.get("https://api.weixin.qq.com/sns/userinfo", pm.getData()));
}
 
Example #28
Source File: MenuApi.java    From jfinal-weixin with Apache License 2.0 4 votes vote down vote up
/**
 * 创建菜单
 */
public static ApiResult createMenu(String jsonStr) {
	String jsonResult = HttpKit.post(createMenu + AccessTokenApi.getAccessTokenStr(), jsonStr);
	return new ApiResult(jsonResult);
}
 
Example #29
Source File: MenuApi.java    From jfinal-weixin with Apache License 2.0 4 votes vote down vote up
/**
 * 查询菜单
 */
public static ApiResult getMenu() {
	String jsonResult = HttpKit.get(getMenu + AccessTokenApi.getAccessTokenStr());
	return new ApiResult(jsonResult);
}
 
Example #30
Source File: QrcodeApi.java    From jfinal-weixin with Apache License 2.0 4 votes vote down vote up
public static ApiResult create(String jsonStr) {
	String jsonResult = HttpKit.post(apiUrl + AccessTokenApi.getAccessTokenStr(), jsonStr);
	return new ApiResult(jsonResult);
}