用HttpClient模拟curl工具,实现企业微信机器人发送数据

要发送的数据格式
用HttpClient模拟curl工具,实现企业微信机器人发送数据_第1张图片
代码

public class Robot {
	 public static String demo(String url,String data) throws ClientProtocolException, IOException{
//	        HttpClient httpClient = new DefaultHttpClient();
	        CloseableHttpClient httpClient = HttpClients.createDefault();
	        // 创建POST请求对象  
	        
	        HttpPost httpPost =  new HttpPost(url);
	        /*
             * 添加请求头信息
             */

	        httpPost.addHeader("Content-Type", "application/json");
	        //设置请求参数
	
	        Robot.setPostData(httpPost, data);
	        
	        HttpResponse httpResponse = httpClient.execute(httpPost);//发送请求
	        HttpEntity httpEntity = httpResponse.getEntity();//获取请求返回体
	        String backResult =  EntityUtils.toString(httpEntity,"UTF-8");//请求返回结果
	        System.out.print(backResult);
	        if(httpResponse != null){
	          try{
	            EntityUtils.consume(httpResponse.getEntity());
	            }catch(IOException e){
	                e.printStackTrace();
	            }
	        }//释放资源
	        
	        return backResult;
	        
	        
	    }
	    
	    //设置请求参数
	    private static void setPostData(HttpPost httpPost, String data){
//	        /*另外一种设置请求参数方法*/
//	        List list = new ArrayList();
//	        for (Map.Entry entry : params.entrySet()) {
//	            list.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
//	        }
//	        HttpEntity httpEntity = null;
	        StringEntity stringEntity = null;
	        try {
	        	stringEntity = new StringEntity(data, "UTF-8");
//	            httpEntity = new UrlEncodedFormEntity(list, "UTF-8");
	            
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        stringEntity.setContentEncoding("UTF-8");    
	        stringEntity.setContentType("application/json");  
	        httpPost.setEntity(stringEntity);//设置请求主体
	    }
	    
	    

        public static void main(String[] args) {
        	  //传递Map params参数格式
    	    TextMessage textMessage = new TextMessage();
    	    textMessage.setContent("hello world");
//    	    ["***","@all"]
    	    String arrStrings[] = {"*******"}; 
    	    textMessage.setMentioned_mobile_list(arrStrings);
    	    String jsonMap = Utils.TextMessage(textMessage);
    	    try {
				Robot.demo("机器人webhook地址", jsonMap);
			} catch (ClientProtocolException e) {
				System.err.println("Http协议出现问题");
				e.printStackTrace();
			} catch (IOException e) {
				System.err.println("IO异常");
				e.printStackTrace();
			}
		}
	  
}

public class TextMessage {
     private String content;
     private String[] mentioned_list;
     private String[] mentioned_mobile_list;
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String[] getMentioned_list() {
		return mentioned_list;
	}
	public void setMentioned_list(String[] mentioned_list) {
		this.mentioned_list = mentioned_list;
	}
	public String[] getMentioned_mobile_list() {
		return mentioned_mobile_list;
	}
	public void setMentioned_mobile_list(String[] mentioned_mobile_list) {
		this.mentioned_mobile_list = mentioned_mobile_list;
	}
	
}


public class Utils {
     public static String TextMessage(TextMessage textMessage) {
    	Map<String, Object> map = new LinkedHashMap<String, Object>();
  	    map.put("msgtype", "text");
  	    Map<String,Object> map1 = new LinkedHashMap<String, Object>();
  	    if(textMessage.getContent()!=null) {
  	    	 map1.put("content", textMessage.getContent());
  	    }
  	    if(textMessage.getMentioned_list()!=null) {
  	    	 map1.put("mentioned_list", textMessage.getMentioned_list());
  	    }
  	    if(textMessage.getMentioned_mobile_list()!=null) {
  	    	 map1.put("mentioned_mobile_list", textMessage.getMentioned_mobile_list());
  	    }
  	    map.put("text", map1);
  	    String jsonMap = JSON.toJSON(map).toString();
  	    return jsonMap;
     }

httpclient需要的jar包
fastjson的jar包
StringEntity和UrlEncodedFormEntity的区别

你可能感兴趣的:(Java模拟curl工具,实现方法httpclient)