public class erweima extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
int userId=123;
try {
AccessToken accessToken = WxUtlis.getAccessToken();
String url="https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="+accessToken.getToken();
String param = "{\"action_name\": \"QR_LIMIT_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": "
+ userId + "" + "}}}";
JSONObject doPoStr = doPoStr(url,param);
System.out.println(doPoStr);
String ticket = (String) doPoStr.get("ticket");
//String wxurl = (String) doPoStr.get("url");
//String newUrl=wxurl.replace(wxurl, "http://www.baidu.com");
//System.out.println(newUrl);
// if(如果是为关注){
// 不替换url
// }else{
// 替换url
// }
req.setAttribute("ticket", ticket);
req.getRequestDispatcher("ticket.jsp").forward(req, resp);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static JSONObject doPoStr(String url,String param) throws Exception{
DefaultHttpClient client=new DefaultHttpClient();
HttpPost httpPost=new HttpPost(url);
httpPost.setHeader("Content-type", "application/json");
JSONObject jsonObject=null;
httpPost.setEntity(new StringEntity(param,"UTF-8"));
HttpResponse httpResponse = client.execute(httpPost);
String request = EntityUtils.toString(httpResponse.getEntity(),"UTF-8");
jsonObject=JSONObject.fromObject(request);
System.out.println(jsonObject);
String newUri = (String) jsonObject.get("url");
String newTicket = (String) jsonObject.get("ticket");
String newsUril=newUri.replace(newUri,"http://www.baidu.com");
System.out.println(newsUril);
String news="{"+"\""+"ticket"+"\""+":"+"\""+newTicket+"\""+","+"\""+"url"+"\""+":"+"\""+newsUril+"\""+"}";
System.out.println(news);
jsonObject=null;
jsonObject=JSONObject.fromObject(news);
System.out.println(jsonObject);
return jsonObject;
}
}