简单http接口对接微信功能DEMO,仅供参考

本文提供了一个使用Java实现的微信客服系统的API调用示例,包括如何通过HTTP客户端发送GET请求来获取客服人员列表。此外,还展示了如何配置请求超时、解析响应结果等实用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.config.RequestConfig;
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();
        RequestConfig rc = RequestConfig.custom().setSocketTimeout(10000).setConnectionRequestTimeout(10000).setConnectTimeout(10000).build();
        CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(rc).build();
		
		//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时长/1000
		JSONObject obj = new JSONObject();
		obj.put("endtime",d2.getTime()/1000);
		obj.put("pageindex",1);
		obj.put("pagesize",10);
		obj.put("starttime",d1.getTime()/1000);
		
		
		//post数据为JSON
		StringEntity 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();
		
	}

}

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

简单就好-怒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值