企业微信对接接口

时间:2025-03-10 09:12:42
public static String doPost( String url, String param) { HttpURLConnection conn = null; OutputStream os = null; InputStreamReader isr = null; String response = ""; try { try { URL wsUrl = new URL(url); conn = (HttpURLConnection) wsUrl.openConnection(); conn.setDoInput(true); // 获取服务端的响应 conn.setDoOutput(true); // 向服务器发送数据 conn.setUseCaches(false); conn.setReadTimeout(10000); //读取超时 conn.setConnectTimeout(10000); //服务器响应超时 conn.setRequestMethod("POST"); // 请求方式 conn.setUseCaches(false); // Post 请求不能使用缓存 conn.setInstanceFollowRedirects(true); //设置本次连接是否自动重定向 conn.setRequestProperty("Content-Type", "application/json"); conn.connect(); os = conn.getOutputStream(); if (param!=null) os.write(param.getBytes()); os.flush(); } catch (Exception e) { e.printStackTrace(); } finally { if (os != null) os.close(); } // 获得响应状态码̬ int resultCode = conn.getResponseCode(); if (HttpURLConnection.HTTP_OK == resultCode) { char[] chr = new char[1024]; isr = new InputStreamReader(conn.getInputStream(),"UTF-8"); int readSize = isr.read(chr); if (readSize > 0) { response = new String(chr, 0, readSize); } } } catch (Exception e) { e.printStackTrace(); } finally { try { if (isr != null) isr.close(); if (conn != null) conn.disconnect(); } catch (Exception e2) { e2.printStackTrace(); } } return response; }