package Model;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import net.sf.json.JSONObject;
public class WxUtil {
public static String access_token;
//主动调用:请求token的时间
public static Date access_token_date;
public JSONObject sendReqMsg(ReqBaseMsg mess)
{
//消息JSON格式
String jsonContext = mess.toJsonStr();
JSONObject result = null;
//获得Tokedn
String token = getTokenFromWx();
System.out.println(token);
boolean flag = false;
try
{
//创建一个httpClient链接:特别注意:此处需要引入一个额外的jar包common-logging.jar
CloseableHttpClient httpclient = HttpClients.createDefault();
//需要访问的链接
HttpPost httpPost = new HttpPost("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+token);
//发送JSON格式的数据
StringEntity myEntity = new StringEntity(jsonContext,ContentType.create("text/plain","UTF-8"));
//设置需要传递的的数据
httpPost.setEntity(myEntity);
//Create a custom response handler
ResponseHandler<JSONObject> responseHandler = new ResponseHandler<JSONObject>()
{
//对访问结果进行处理
public JSONObject handleResponse(final HttpResponse response)throws ClientProtocolException,IOException
{
int status = response.getStatusLine().getStatusCode();
if(status>=200&&status<300)
{
HttpEntity entity = response.getEntity();
if(null!=entity)
{
String result = EntityUtils.toString(entity);
//根据字符串生成JSON对象
JSONObject resultObj = JSONObject.fromObject(result);
return resultObj;
}
else
{
return null;
}
}
else
{
throw new ClientProtocolException("Unexpected response status:"+status);
}
}
};
//返回的JSON对象
JSONObject responseBody = httpclient.execute(httpPost,responseHandler);
//System.out.println(responseBody.toString());
result = responseBody;
httpclient.close();
}catch(Exception e)
{
e.printStackTrace();
}
return result;
}
public JSONObject sendReqMsg(String jsonContext)
{
JSONObject result = null;
//获得Tokedn
String token = getTokenFromWx();
boolean flag = false;
try
{
//创建一个httpClient链接:特别注意:此处需要引入一个额外的jar包common-logging.jar
CloseableHttpClient httpclient = HttpClients.createDefault();
//需要访问的链接
HttpPost httpPost = new HttpPost("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+token);
//发送JSON格式的数据
StringEntity myEntity = new StringEntity(jsonContext,ContentType.create("text/plain","UTF-8"));
//设置需要传递的的数据
httpPost.setEntity(myEntity);
//Create a custom response handler
ResponseHandler<JSONObject> responseHandler = new ResponseHandler<JSONObject>()
{
//对访问结果进行处理
public JSONObject handleResponse(final HttpResponse response)throws ClientProtocolException,IOException
{
int status = response.getStatusLine().getStatusCode();
if(status>=200&&status<300)
{
HttpEntity entity = response.getEntity();
if(null!=entity)
{
String result = EntityUtils.toString(entity);
//根据字符串生成JSON对象
JSONObject resultObj = JSONObject.fromObject(result);
return resultObj;
}
else
{
return null;
}
}
else
{
throw new ClientProtocolException("Unexpected response status:"+status);
}
}
};
//返回的JSON对象
JSONObject responseBody = httpclient.execute(httpPost,responseHandler);
//System.out.println(responseBody.toString());
result = responseBody;
httpclient.close();
}catch(Exception e)
{
e.printStackTrace();
}
return result;
}
public String getTokenFromWx()
{
//微信企业号标识
String corpid="wwb29700f48615bc97";
//管理组凭证密钥
String corpsecret="60vGMfmSuZBBCcjucpHuHyK9wzgtnKOcEQn_vH32OXE";
//获取的标识
String token="";
//1、判断access_token是否存在,不存在的话直接申请
//2、判断时间是否过期,过期(>=7200秒)申请,否则不用请求直接返回以后的token
if(null==access_token||"".equals(access_token)||(new Date().getTime()-access_token_date.getTime())>=(7000*1000)){
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
//利用get形式获得token
HttpGet httpget = new HttpGet("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+corpid+"&corpsecret="+corpsecret);
// Create a custom response handler
ResponseHandler<JSONObject> responseHandler = new ResponseHandler<JSONObject>() {
public JSONObject handleResponse(
final HttpResponse response) throws ClientProtocolException, IOException {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
if(null!=entity){
String result= EntityUtils.toString(entity);
//根据字符串生成JSON对象
JSONObject resultObj = JSONObject.fromObject(result);
return resultObj;
}else{
return null;
}
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
}
};
//返回的json对象
JSONObject responseBody = httpclient.execute(httpget, responseHandler);
if(null!=responseBody){
token= (String) responseBody.get("access_token");//返回token
}
httpclient.close();
//设置全局变量
access_token=token;
access_token_date=new Date();
}catch (Exception e) {
e.printStackTrace();
}
}else{
token=access_token;
}
return token;
}
//文件上传的微信服务器
//fileType文件类型分别有image/voice/video/file(普通文件)
public JSONObject send(String fileType,String filePath) throws Exception
{
String result = null;
File file = new File(filePath);
if(!file.exists()||!file.isFile())
{
throw new IOException("文件不存在");
}
String token = getTokenFromWx();
//第一部分
URL urlObj = new URL("https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token="+token+"&type="+fileType+"");
HttpURLConnection con = (HttpURLConnection) urlObj.openConnection();
con.setRequestMethod("POST");//以POST方式提交表单,默认GET方式
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);//POS