简单http接口对接微信功能DEMO,仅供参考
来源:互联网 发布:数据库截断 编辑:程序博客网 时间:2024/05/17 22:59
package demo;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileWriter;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import org.apache.http.HttpEntity;import org.apache.http.client.methods.CloseableHttpResponse;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.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.json.JSONArray;import org.json.JSONObject;public class HttpDemo {/** * @param args */public static void main(String[] args) throws Exception{String appId="";String secret="";String token="";String openId="";//一个用户的openID//获取access_token//String url= "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+secret+"";//获取用户列表//String url="https://api.weixin.qq.com/cgi-bin/user/get?access_token="+token;//获取单个用户信息//String url="https://api.weixin.qq.com/cgi-bin/user/info?access_token="+token+"&openid="+openId+"&lang=zh_CN";//获取批量用户信息//String url="https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token="+token;//菜单//String url="https://api.weixin.qq.com/cgi-bin/menu/get?access_token="+token;//获取所有客服信息String url="https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token="+token;//客服向用户发送消息//String url="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+token;//获取客服的聊天记录//String url="https://api.weixin.qq.com/customservice/msgrecord/getrecord?access_token="+token;////String url="";/*******************httpclient4****************************/CloseableHttpClient client = HttpClients.createDefault();//get请求HttpGet get = new HttpGet(url);//post请求//HttpPost post = new HttpPost(url);/*File file = new File("f://test/openid.txt");InputStream ins =new FileInputStream(file);StringBuffer sb = new StringBuffer();byte[] bt = new byte[1024];int le = 0;while((le = ins.read(bt))!=-1){sb.append(new String(bt,0,le));} JSONObject j = new JSONObject(sb.toString()); JSONObject s = (JSONObject) j.get("data"); JSONArray a = (JSONArray) s.get("openid"); JSONObject obj=new JSONObject();JSONArray arr=new JSONArray();for(int i=0;i<a.length();i++){JSONObject subObj=new JSONObject(); subObj.put("openid",a.get(i)); subObj.put("lang","zh-CN"); arr.put(subObj);}obj.put("user_list", arr);*//*JSONObject obj = new JSONObject();JSONObject obj2= new JSONObject();obj.put("touser","openid");obj.put("msgtype","text");obj2.put("content","微信测试:客服发送消息给用户");obj.put("text",obj2);*//*Calendar start=Calendar.getInstance();Calendar end=Calendar.getInstance();start.set(2016, 4, 1,0,0,0);end.set(2016, 4, 25,0,0,0);*//*SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date d1=format.parse("2016-4-1 00:00:00");Date d2=format.parse("2016-4-25 00:00:00");//UNIX时间戳,long时长/1000JSONObject obj = new JSONObject();obj.put("endtime",d2.getTime()/1000);obj.put("pageindex",1);obj.put("pagesize",10);obj.put("starttime",d1.getTime()/1000);//post数据为JSONStringEntity se = new StringEntity(obj.toString(),"utf-8");se.setContentEncoding("utf-8");se.setContentType("application/json");post.setEntity(se);*///post数据为key=value /*List<NameValuePair> list = new ArrayList<NameValuePair>(); list.add(new BasicNameValuePair("name","value"));UrlEncodedFormEntity ue = new UrlEncodedFormEntity(list,Consts.UTF_8);post.setEntity(ue);*/CloseableHttpResponse response= client.execute(get);//CloseableHttpResponse response = client.execute(post);HttpEntity entity = response.getEntity();if(entity != null){ InputStream is = entity.getContent(); /* OutputStream os = new FileOutputStream(new File("f:/test/abc.txt"));byte[] b = new byte[1024];int len = 0;while((len = is.read(b)) != -1){//System.out.println(new String(b,0,len,"utf-8"));os.write(b);}os.flush();os.close();is.close();*/ //用它的好处是应该是没有乱码BufferedReader br = new BufferedReader(new InputStreamReader(is));//BufferedWriter bw = new BufferedWriter(new FileWriter("f:/test/abc.txt"));//bw.write(br.readLine());System.out.println(br.readLine());while(br.read() != -1){System.out.println(br.readLine());//bw.write(br.readLine());}//bw.flush();//bw.close();br.close();//强烈不建议//long len=entity.getContentLength();//if(len!=-1&&len<1024){//System.out.println(EntityUtils.toString(entity));//}}response.close();}}
0 0
- 简单http接口对接微信功能DEMO,仅供参考
- java 服务端对接微信支付 Demo
- Django 微信公众号对接开发demo
- php对接app微信支付的请求接口
- C#微信公众号支付接口对接
- 对接微信平台
- 微信开发对接
- 微信对接
- 微信开发---对接
- ionic微信对接
- 微信jsApiSdk对接
- 微信高级群发接口demo
- 微信接口开发demo(C#)
- 微信高级群发接口demo
- 微信现金红包接口demo(java)
- 微信----简单自动回复功能emoji和文本处理demo
- C#处理和对接HTTP接口请求
- ecshop 对接 微信商城
- objc--NSUInteger and INT 转换问题
- android+zip4j开源项目问题汇总
- android--系统路径获取
- hdu1251 统计难题(字典树)
- android--可循环显示图像的Android Gallery组件
- 简单http接口对接微信功能DEMO,仅供参考
- android--下拉列表 Spinner。
- android--Java基于Socket文件传输示例
- android--imageView使用sd卡中图片资源
- OA实战项目
- Android--toast使用
- java-socket传输zip文件实例
- IT各种百度云资料
- android--在sd卡中创建文件夹