增加公众号推送功能代码!
This commit is contained in:
		@@ -32,5 +32,15 @@
 | 
			
		||||
        <SOURCES />
 | 
			
		||||
      </library>
 | 
			
		||||
    </orderEntry>
 | 
			
		||||
    <orderEntry type="module-library" scope="TEST">
 | 
			
		||||
      <library name="JUnit4">
 | 
			
		||||
        <CLASSES>
 | 
			
		||||
          <root url="jar://$MODULE_DIR$/lib/junit-4.13.1.jar!/" />
 | 
			
		||||
          <root url="jar://$MODULE_DIR$/lib/hamcrest-core-1.3.jar!/" />
 | 
			
		||||
        </CLASSES>
 | 
			
		||||
        <JAVADOC />
 | 
			
		||||
        <SOURCES />
 | 
			
		||||
      </library>
 | 
			
		||||
    </orderEntry>
 | 
			
		||||
  </component>
 | 
			
		||||
</module>
 | 
			
		||||
@@ -21,6 +21,8 @@
 | 
			
		||||
    </bytecodeTargetLevel>
 | 
			
		||||
  </component>
 | 
			
		||||
  <component name="Encoding" defaultCharsetForPropertiesFiles="UTF-8">
 | 
			
		||||
    <file url="file://$PROJECT_DIR$/src/main/java/test/com/cpic/xim/wechat/officalAccount/PushMessageTest.java" charset="GBK" />
 | 
			
		||||
    <file url="file://$PROJECT_DIR$/src/test/java/com/cpic/xim/wechat/officalAccount/PushMessageTest.java" charset="UTF-8" />
 | 
			
		||||
    <file url="PROJECT" charset="UTF-8" />
 | 
			
		||||
  </component>
 | 
			
		||||
  <component name="ExportToHTMLSettings">
 | 
			
		||||
 
 | 
			
		||||
@@ -1,10 +1,18 @@
 | 
			
		||||
 | 
			
		||||
package com.cpic.xim.wechat.officalAccount;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.annotation.JsonInclude;
 | 
			
		||||
import com.fasterxml.jackson.annotation.JsonProperty;
 | 
			
		||||
import com.fasterxml.jackson.core.JsonProcessingException;
 | 
			
		||||
import com.fasterxml.jackson.databind.ObjectMapper;
 | 
			
		||||
 | 
			
		||||
import java.io.*;
 | 
			
		||||
 | 
			
		||||
import java.net.MalformedURLException;
 | 
			
		||||
import java.net.URL;
 | 
			
		||||
import java.net.HttpURLConnection;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
public class PushMessage
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
@@ -22,24 +30,97 @@ public class PushMessage
 | 
			
		||||
                                          String notifyMessage )
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        //设置推送内容
 | 
			
		||||
        WechatOfficalAccountMessageParameter param = new WechatOfficalAccountMessageParameter();
 | 
			
		||||
 | 
			
		||||
        //param.setUrl( url );
 | 
			
		||||
        param.setFirst( title );
 | 
			
		||||
        param.setKeyword1( notifyType );
 | 
			
		||||
        param.setKeyword1Color( "#ff000000" );
 | 
			
		||||
        param.setKeyword2( notifyMessage );
 | 
			
		||||
 | 
			
		||||
        ObjectMapper mapper = new ObjectMapper();
 | 
			
		||||
        //转换成json
 | 
			
		||||
        ObjectMapper mapper = null;
 | 
			
		||||
        String       json   = null;
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            String json = mapper.writeValueAsString( param );
 | 
			
		||||
            mapper = new ObjectMapper();
 | 
			
		||||
            mapper.setSerializationInclusion( JsonInclude.Include.NON_NULL );
 | 
			
		||||
 | 
			
		||||
            json = mapper.writeValueAsString( param );
 | 
			
		||||
 | 
			
		||||
            System.out.println( json );
 | 
			
		||||
        }
 | 
			
		||||
        catch ( JsonProcessingException error )
 | 
			
		||||
        {
 | 
			
		||||
            
 | 
			
		||||
            error.printStackTrace();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        //推送数据
 | 
			
		||||
        URL               url        = null;
 | 
			
		||||
        HttpURLConnection connection = null;
 | 
			
		||||
 | 
			
		||||
        StringBuffer       result = new StringBuffer();
 | 
			
		||||
        OutputStreamWriter out    = null;
 | 
			
		||||
        BufferedReader     in     = null;
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            url        = new URL( wechatOfficalAccountURL );
 | 
			
		||||
            connection = (HttpURLConnection) url.openConnection();
 | 
			
		||||
 | 
			
		||||
            connection.setRequestMethod( "POST" );
 | 
			
		||||
            connection.setRequestProperty( "accept", "*/*" );
 | 
			
		||||
            connection.setRequestProperty( "Connection", "Keep-Alive" );
 | 
			
		||||
            connection.setRequestProperty( "Content-Type", "application/json" );
 | 
			
		||||
            connection.setDoInput( true );
 | 
			
		||||
            connection.setDoOutput( true );
 | 
			
		||||
            connection.connect();
 | 
			
		||||
 | 
			
		||||
            out = new OutputStreamWriter( connection.getOutputStream(), "UTF-8" );
 | 
			
		||||
            out.write( json );
 | 
			
		||||
            out.flush();
 | 
			
		||||
 | 
			
		||||
            in = new BufferedReader( new InputStreamReader( connection.getInputStream() ) );
 | 
			
		||||
 | 
			
		||||
            String line = in.readLine();
 | 
			
		||||
 | 
			
		||||
            while ( line != null )
 | 
			
		||||
            {
 | 
			
		||||
                result.append( line );
 | 
			
		||||
 | 
			
		||||
                line = in.readLine();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        catch ( Exception error )
 | 
			
		||||
        {
 | 
			
		||||
            error.printStackTrace();
 | 
			
		||||
        }
 | 
			
		||||
        finally
 | 
			
		||||
        {
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                if ( in != null )
 | 
			
		||||
                {
 | 
			
		||||
                    in.close();
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                if ( out != null )
 | 
			
		||||
                {
 | 
			
		||||
                    out.close();
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                if ( connection != null )
 | 
			
		||||
                {
 | 
			
		||||
                    connection.disconnect();
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            catch ( Exception error )
 | 
			
		||||
            {
 | 
			
		||||
                error.printStackTrace();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,24 @@
 | 
			
		||||
package com.cpic.xim.wechat.officalAccount;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.core.JsonProcessingException;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
 | 
			
		||||
import static org.junit.Assert.*;
 | 
			
		||||
 | 
			
		||||
public class PushMessageTest
 | 
			
		||||
{
 | 
			
		||||
    @Test
 | 
			
		||||
    public void pushNotifyMessage()
 | 
			
		||||
    {
 | 
			
		||||
        String url = "https://cxxmwx.cpic.com.cn/app/index.php?i=2&c=entry&do=send_group_tpl_api&m=ok_tplmessage";
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            PushMessage.PushNotifyMessage( url, "警报", "警报标题", "警报内容!" );
 | 
			
		||||
        }
 | 
			
		||||
        catch ( Exception error  )
 | 
			
		||||
        {
 | 
			
		||||
            fail("测试失败!");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										171
									
								
								文档/example.java
									
									
									
									
									
								
							
							
						
						
									
										171
									
								
								文档/example.java
									
									
									
									
									
								
							@@ -2,28 +2,26 @@
 | 
			
		||||
 * @Author: Kane
 | 
			
		||||
 * @Date: 2022-04-18 11:37:33
 | 
			
		||||
 * @LastEditors: Kane
 | 
			
		||||
 * @LastEditTime: 2022-04-18 11:41:09
 | 
			
		||||
 * @LastEditTime: 2022-04-18 15:58:34
 | 
			
		||||
 * @FilePath: \undefinedd:\develop\产险厦门分公司项目\天气灾害预警\文档\example.java
 | 
			
		||||
 * @Description: 
 | 
			
		||||
 * 
 | 
			
		||||
 * Copyright (c) ${2022} by Kane, All Rights Reserved. 
 | 
			
		||||
 */
 | 
			
		||||
import org.apache.commons.httpclient.HttpClient;
 | 
			
		||||
 | 
			
		||||
import org.apache.httpcomponents.httpclient.HttpClient;
 | 
			
		||||
import org.apache.commons.httpclient.HttpStatus;
 | 
			
		||||
import org.apache.commons.httpclient.methods.PostMethod;
 | 
			
		||||
import org.apache.commons.httpclient.methods.StringRequestEntity;
 | 
			
		||||
 | 
			
		||||
public class HttpUtils {
 | 
			
		||||
    public static String sendPostWithJson(String url, String jsonStr, HashMap<String,String> headers) {
 | 
			
		||||
    public static String sendPostWithJson(String url, String jsonStr, HashMap<String, String> headers) {
 | 
			
		||||
        // 返回的结果
 | 
			
		||||
        String jsonResult = "";
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            HttpClient client = new HttpClient();
 | 
			
		||||
 | 
			
		||||
            // 连接超时
 | 
			
		||||
               client.getHttpConnectionManager().getParams().setConnectionTimeout(3*1000);
 | 
			
		||||
            client.getHttpConnectionManager().getParams().setConnectionTimeout(3 * 1000);
 | 
			
		||||
            // 读取数据超时
 | 
			
		||||
               client.getHttpConnectionManager().getParams().setSoTimeout(3*60*1000);
 | 
			
		||||
            client.getHttpConnectionManager().getParams().setSoTimeout(3 * 60 * 1000);
 | 
			
		||||
            client.getParams().setContentCharset("UTF-8");
 | 
			
		||||
 | 
			
		||||
            PostMethod postMethod = new PostMethod(url);
 | 
			
		||||
@@ -31,9 +29,9 @@ public class HttpUtils {
 | 
			
		||||
            postMethod.setRequestHeader("content-type", headers.get("content-type"));
 | 
			
		||||
 | 
			
		||||
            // 非空
 | 
			
		||||
               if (null != jsonStr && !"".equals(jsonStr))
 | 
			
		||||
               {
 | 
			
		||||
                   StringRequestEntity requestEntity = new StringRequestEntity(jsonStr, headers.get("content-type"), "UTF-8");
 | 
			
		||||
            if (null != jsonStr && !"".equals(jsonStr)) {
 | 
			
		||||
                StringRequestEntity requestEntity = new StringRequestEntity(jsonStr, headers.get("content-type"),
 | 
			
		||||
                        "UTF-8");
 | 
			
		||||
                postMethod.setRequestEntity(requestEntity);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
@@ -51,8 +49,7 @@ public class HttpUtils {
 | 
			
		||||
        return jsonResult;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void main(String[] args) 
 | 
			
		||||
    {
 | 
			
		||||
    public static void main(String[] args) {
 | 
			
		||||
        String requestUrl = "http://localhost:8070/test/rz/server/rzxx/at_VaildToken.do";
 | 
			
		||||
        String jsonStr = "{\"name\":\"张三\"}";
 | 
			
		||||
 | 
			
		||||
@@ -60,11 +57,155 @@ public class HttpUtils {
 | 
			
		||||
        headers.put("content-type", "application/json");
 | 
			
		||||
 | 
			
		||||
        // 发送post请求
 | 
			
		||||
        String resultData = HttpUtils.sendPostWithJson(requestUrl, jsonStr,headers);
 | 
			
		||||
        String resultData = HttpUtils.sendPostWithJson(requestUrl, jsonStr, headers);
 | 
			
		||||
 | 
			
		||||
        // 并接收返回结果
 | 
			
		||||
        System.out.println(resultData);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 
 | 
			
		||||
     * @author : cjd
 | 
			
		||||
     * 
 | 
			
		||||
     * @description : post接口 返回结果字符串
 | 
			
		||||
     * 
 | 
			
		||||
     * @params : [url, param]
 | 
			
		||||
     * 
 | 
			
		||||
     * @param url   请求接口
 | 
			
		||||
     * 
 | 
			
		||||
     * @param param 需要的json字符串
 | 
			
		||||
     * 
 | 
			
		||||
     * @return :java.lang.String
 | 
			
		||||
     * 
 | 
			
		||||
     * @date : 17:31 2018/8/1
 | 
			
		||||
     * 
 | 
			
		||||
     */
 | 
			
		||||
 | 
			
		||||
    public static String sendPost(String url, String param) 
 | 
			
		||||
    {
 | 
			
		||||
        OutputStreamWriter out = null;
 | 
			
		||||
        BufferedReader in = null;
 | 
			
		||||
 | 
			
		||||
        String result = "";
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            URL realUrl = new URL(url);
 | 
			
		||||
            HttpURLConnection conn = null; // 打开和URL之间的连接
 | 
			
		||||
 | 
			
		||||
            conn = (HttpURLConnection) realUrl.openConnection(); // 发送POST请求必须设置如下两行
 | 
			
		||||
            conn.setDoOutput(true);
 | 
			
		||||
            conn.setDoInput(true);
 | 
			
		||||
            conn.setRequestMethod("POST"); // POST方法
 | 
			
		||||
 | 
			
		||||
            // 设置通用的请求属性
 | 
			
		||||
            conn.setRequestProperty("accept", "*/*");
 | 
			
		||||
            conn.setRequestProperty("connection", "Keep-Alive");
 | 
			
		||||
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
 | 
			
		||||
            conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
 | 
			
		||||
            conn.connect(); // 获取URLConnection对象对应的输出流
 | 
			
		||||
 | 
			
		||||
            out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8"); // 发送请求参数
 | 
			
		||||
            out.write(param); // flush输出流的缓冲
 | 
			
		||||
            out.flush(); // 定义BufferedReader输入流来读取URL的响应
 | 
			
		||||
 | 
			
		||||
            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
 | 
			
		||||
 | 
			
		||||
            String line;
 | 
			
		||||
            while ((line = in.readLine()) != null) 
 | 
			
		||||
            {
 | 
			
		||||
                result += line;
 | 
			
		||||
            }
 | 
			
		||||
        } 
 | 
			
		||||
        catch (Exception e) 
 | 
			
		||||
        {
 | 
			
		||||
            System.out.println("发送 POST 请求出现异常!" + e);
 | 
			
		||||
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
 | 
			
		||||
        } // 使用finally块来关闭输出流、输入流
 | 
			
		||||
        finally {
 | 
			
		||||
            try {
 | 
			
		||||
                if (out != null)
 | 
			
		||||
                {
 | 
			
		||||
                    out.close();
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                if (in != null) 
 | 
			
		||||
                {
 | 
			
		||||
                    in.close();
 | 
			
		||||
                }
 | 
			
		||||
            } 
 | 
			
		||||
            catch (IOException ex) 
 | 
			
		||||
            {
 | 
			
		||||
                ex.printStackTrace();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return result;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
public String postMethod(String url,String param){
 | 
			
		||||
    // 结果值
 | 
			
		||||
    StringBuffer rest=new StringBuffer();
 | 
			
		||||
 | 
			
		||||
    HttpURLConnection conn=null;
 | 
			
		||||
    OutputStream out=null;
 | 
			
		||||
    BufferedReader br=null;
 | 
			
		||||
 | 
			
		||||
    try {
 | 
			
		||||
        // 创建 URL
 | 
			
		||||
        URL restUrl = new URL(url);
 | 
			
		||||
        // 打开连接
 | 
			
		||||
        conn= (HttpURLConnection) restUrl.openConnection();
 | 
			
		||||
        // 设置请求方式为 POST
 | 
			
		||||
        conn.setRequestMethod("POST");
 | 
			
		||||
        conn.setRequestProperty("Connection","keep-Alive");
 | 
			
		||||
        // 设置接收文件类型
 | 
			
		||||
//            conn.setRequestProperty("Accept","application/json");
 | 
			
		||||
        //设置发送文件类型
 | 
			
		||||
        /**
 | 
			
		||||
         这里注意  传递JSON数据的话 就要设置
 | 
			
		||||
         普通参数的话 就要注释掉
 | 
			
		||||
         */
 | 
			
		||||
        conn.setRequestProperty("Content-Type","application/json");
 | 
			
		||||
        // 输入 输出 都打开
 | 
			
		||||
        conn.setDoOutput(true);
 | 
			
		||||
        conn.setDoInput(true);
 | 
			
		||||
        //开始连接
 | 
			
		||||
        conn.connect();
 | 
			
		||||
 | 
			
		||||
        // 传递参数  流的方式
 | 
			
		||||
        out=conn.getOutputStream();
 | 
			
		||||
        out.write(param.getBytes());
 | 
			
		||||
        out.flush();
 | 
			
		||||
 | 
			
		||||
        // 读取数据
 | 
			
		||||
        br=new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
 | 
			
		||||
        String line=null;
 | 
			
		||||
        while (null != (line=br.readLine())){
 | 
			
		||||
            rest.append(line);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    } catch (Exception e) {
 | 
			
		||||
        e.printStackTrace();
 | 
			
		||||
    }finally {
 | 
			
		||||
        // 关闭所有通道
 | 
			
		||||
        try {
 | 
			
		||||
            if (br!=null) {
 | 
			
		||||
                br.close();
 | 
			
		||||
            }
 | 
			
		||||
            if (out!=null) {
 | 
			
		||||
                out.close();
 | 
			
		||||
            }
 | 
			
		||||
            if (conn!=null) {
 | 
			
		||||
                conn.disconnect();
 | 
			
		||||
            }
 | 
			
		||||
        } catch (Exception e2) {
 | 
			
		||||
            e2.printStackTrace();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return rest.toString();
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user