通过http获取资源

法一(推荐):

public static String getHttpPost(String url, String params) {

String result = "";
HttpPost httppost = new HttpPost(url);
StringEntity entity = new StringEntity(params, ContentType.create("application/json", Consts.UTF_8));
entity.setChunked(true);
httppost.setEntity(entity);
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response;
try {
response = httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode() == 200) {
result = EntityUtils.toString(response.getEntity(), "UTF-8");
} else {
result = "[返回失败!]";
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;

}

法二:

public static String getMes(String url, String params) {
CloseableHttpClient client = HttpClients.createDefault();
HttpPost htPost = new HttpPost(url);
String backMes = null;
try {
htPost.setHeader("content-type", "application/json");
StringEntity reqEntity = new StringEntity(params);
htPost.setEntity(reqEntity);
HttpResponse httpResponse = client.execute(htPost);
HttpEntity entity = httpResponse.getEntity();
backMes = EntityUtils.toString(entity, "UTF-8").toString();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
client.close();
client = null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return backMes;
}


*应用:生成二维码(框架:playframework,数据库:mongodb)

接口

/**
* 创建临时二维码ticket

* @param access_token
* @return
* @throws Exception
*/
public String getJsapi_ticket_WeixinLs(Long manageQRcodeId) throws Exception {
String jsapi_ticket = "";
CloseableHttpClient httpclient = HttpClients.createDefault();
try {


HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + getCacheAccessToken());


JSONObject jsonObj = new JSONObject();
JSONObject scene = new JSONObject();
JSONObject action_info = new JSONObject();


jsonObj.put("action_name", "QR_SCENE");
jsonObj.put("expire_seconds", 604800);
scene.put("scene_id", manageQRcodeId);
action_info.put("scene", scene);
jsonObj.put("action_info", action_info);


/*
* jsonObj.put( "action_info", new JSONObject().put("scene", new JSONObject().put("scene_id", manageQRcodeId)));
*/


StringEntity entity = new StringEntity(jsonObj.toString(), "UTF-8");


httpPost.setEntity(entity);


CloseableHttpResponse response1 = httpclient.execute(httpPost);
try {


HttpEntity httpEntity = response1.getEntity();


if (httpEntity != null) {
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpEntity.getContent(), "UTF-8"), 8 * 1024);
StringBuilder entityStringBuilder = new StringBuilder();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
entityStringBuilder.append(line);
}
// 利用从HttpEntity中得到的String生成JsonObject
// resultJsonObject = new
// JSONObject(entityStringBuilder.toString());


JSONObject dataJson = JSONObject.fromObject(entityStringBuilder.toString());
jsapi_ticket = dataJson.get("ticket") + "";
} catch (Exception e) {
e.printStackTrace();
}
}


} finally {
response1.close();
}
} finally {
httpclient.close();
}
return jsapi_ticket;


}


/**
* 创建永久二维码ticket

* @param manageQRcodeId
* @return
* @throws Exception
*/
public String getJsapi_ticket_WeixinGd(Long manageQRcodeId) throws Exception {
String jsapi_ticket = "";
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + getCacheAccessToken());


JSONObject jsonObj = new JSONObject();
JSONObject action_info = new JSONObject();
JSONObject scene = new JSONObject();
jsonObj.put("action_name", "QR_LIMIT_SCENE");
scene.put("scene_id", manageQRcodeId);
action_info.put("scene", scene);


jsonObj.put("action_info", action_info);


System.out.println(jsonObj.toString());


StringEntity entity = new StringEntity(jsonObj.toString(), "UTF-8");


httpPost.setEntity(entity);


CloseableHttpResponse response1 = httpclient.execute(httpPost);
try {


HttpEntity httpEntity = response1.getEntity();


if (httpEntity != null) {
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpEntity.getContent(), "UTF-8"), 8 * 1024);
StringBuilder entityStringBuilder = new StringBuilder();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
entityStringBuilder.append(line);
}
// 利用从HttpEntity中得到的String生成JsonObject
JSONObject dataJson = JSONObject.fromObject(entityStringBuilder.toString());
jsapi_ticket = dataJson.get("ticket") + "";
} catch (Exception e) {
e.printStackTrace();
}
}


} finally {
response1.close();
}
} finally {
httpclient.close();
}
return jsapi_ticket;


}

调用

public static Result createChannelPic(String channelNo) {
// 生成返回的json对象
ObjectNode repnode = Json.newObject();
DBCollection coll = MongoManager.getDB("").getCollection("channel_manager");


DBObject query = new BasicDBObject("channel_no", channelNo).append("is_enable", 1);


DBObject channel = coll.findOne(query);
repnode.put("status", 1);
repnode.put("msg", "二维码生成成功!");
if (channel == null) {
repnode.put("status", 0);
repnode.put("msg", "渠道信息不存在!");
} else {
// 获取存放二维码地址
String upload_user_channel = Play.application().configuration().getString("upload_user_channel");
upload_user_channel += channelNo + ".jpg";


Long cn = Long.parseLong(channelNo);


// 获取生产二维码的工具类
QRHttpClient client = QRHttpClient.getInstance();
try {
String ticket = client.getJsapi_ticket_WeixinGd(cn);
client.getticketImage(URLEncoder.encode(ticket, "UTF-8"), upload_user_channel);


// 保存数据到数据库
DBObject o = new BasicDBObject();
o.put("qr_path", upload_user_channel);
o.put("ticket", ticket);
o.put("update_date", new Date());
coll.update(query, new BasicDBObject("$set", o), true, false);


} catch (Exception e) {
repnode.put("status", 0);
repnode.put("msg", "生成失败,请稍后再试!");
}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值