浅谈Java代码的 微信长链转短链接口使用 post 请求封装Json(实例)
作者:jingxian 时间:2023-07-27 19:36:09
废话不多说,直接上代码
String longUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + MpUtil.APPID + "&redirect_uri=" + MpUtil.HOMEPAGE + "/nweixinLoginPc.fo%3Frandomcode=" + randomcode + "&response_type=code&scope=snsapi_userinfo&state=account#wechat_redirect";//什么不重要 ,自己的长链
String accessToken = MpUtil.getAccessToken(MpUtil.APPID, MpUtil.APPSECRET);
String shortUrl = null;//短连接地址,生成二维码用,识别快
String httpUrl = "https://api.weixin.qq.com/cgi-bin/shorturl?access_token="+accessToken; //此处访问微信短链方法
/**
* 调用举例
curl -d "{\"action\":\"long2short\",
\"long_url\":\"http://wap.koudaitong.com/v2/showcase/goods?alias=128wi9shh&spm=h56083&redirect_count=1\"}"
"https://api.weixin.qq.com/cgi-bin/shorturl?access_token=ACCESS_TOKEN"
*/
JSONObject jsonObject = new JSONObject();
jsonObject.put("action", "long2short");
jsonObject.put("long_url", longUrl);
String transJson = jsonObject.toString()+httpUrl;
RequestEntity se = new StringRequestEntity(transJson, "application/json", "UTF-8");
//微信返回的字符串
//成功 {"errcode":0,"errmsg":"ok","short_url":"http:\/\/w.url.cn\/s\/AvCo6Ih"}
//失败 {"errcode":40013,"errmsg":"invalid appid"}
String resultsString = post(jsonObject,httpUrl); //封装的post方法
String shortUrl = MpUtil.getJsonValue(resultsString, "short_url");//得到的短链
?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849public static String post(JSONObject json,String URL) { HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(URL); post.setHeader("Content-Type", "application/json"); post.addHeader("Authorization", "Basic YWRtaW46"); String result = ""; try { StringEntity s = new StringEntity(json.toString(), "utf-8"); s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); post.setEntity(s); // 发送请求 HttpResponse httpResponse = client.execute(post); // 获取响应输入流 InputStream inStream = httpResponse.getEntity().getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader( inStream, "utf-8")); StringBuilder strber = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) strber.append(line + "\n"); inStream.close(); result = strber.toString(); if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { return result; } else { result=""; } } catch (Exception e) { System.out.println("请求异常"); throw new RuntimeException(e); } return result; }
获取参数 结果的
?12345678910111213141516/** * 获取json中的值 * @param json * @param key * @return */ public static String getJsonValue(String json, String key) { String value = ""; try { JSONObject jsonObj = new JSONObject(json); value = jsonObj.getString(key); } catch (Exception e) { value = ""; } return value; }
标签:微信,长链转短链,post,请求,封装Json
0
投稿
猜你喜欢
Java基础教程之static五大应用场景
2023-11-11 05:10:43
MyBatis框架之mybatis逆向工程自动生成代码
2023-01-11 06:19:18
使用java生成字母验证码
2021-10-29 23:50:25
JavaWeb项目部署到服务器详细步骤详解
2023-11-29 11:15:20
详解C#中的委托
2022-05-10 06:01:43
Springboot如何使用filter对request body参数进行校验
2023-09-14 05:28:46
c# 如何实现不同进程之间的通信
2022-08-26 10:25:06
springboot vue 跨域问题的解决
2023-01-26 00:53:57
IDEA-Maven项目的jdk版本设置方法
2022-07-21 16:04:17
C# 中的动态创建组件(属性及事件)的实现思路及方法
2021-07-20 04:58:31
Android 利用ViewPager+GridView实现首页导航栏布局分页效果
2021-08-05 09:39:07
详解IDEA启动多个微服务的配置方法
2023-11-24 09:22:24
Java中的回调
2023-11-16 01:55:58
Java实现为Word每一页设置不同图片水印的效果
2023-01-30 07:10:04
JavaWeb中JavaMail创建邮件和发送邮件
2022-01-29 02:54:09
初学C#所需明白的那些点
2023-11-26 14:40:50
在C#中根据HardwareID获取驱动程序信息的实现代码
2023-08-04 08:15:31
详细解读Hibernate的缓存机制
2023-05-03 21:02:52
java 一个类实现两个接口的案例
2023-08-09 12:24:35
java Class.getSimpleName() 详解及用法
2021-06-26 16:35:41