收藏
回答

第一次运行正常,可以返回会Token,再次运行就报错了,要等几个小时,再运行又正常,然后又报错

问题类型 API/组件名称 终端类型 微信版本 基础库版本
需求 gettoken 微信安卓客户端 3.0.30.2006 2.0.0

发送GET请求出现异常!java.net.SocketException: recv() failed, errno = 104

Connection reset by peer

java.net.SocketException: recv() failed, errno = 104

Connection reset by peer

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java)

at java.lang.reflect.Constructor.newInstance(Constructor.java)

at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1514)

at java.security.AccessController.doPrivileged(Native Method)

at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1508)

at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java)

at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java)

at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:318)

at QyAPI_WeiXin_Send_Message.QyAPI_WeiXin_Send_Message.sendGet(QyAPI_WeiXin_Send_Message.java:57)

at QyAPI_WeiXin_Send_Message.QyAPI_WeiXin_Send_Message.main(QyAPI_WeiXin_Send_Message.java:169)

at cux_wx_send_msg.main(CUX_WX_SEND_MSG:6)

create or replace and compile java source named qyapi_weixin_send_message as import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.URL; import java.util.List; import java.util.Map; import java.net.HttpURLConnection; import org.json.JSONException; import org.json.JSONObject; public class QyAPI_WeiXin_Send_Message { /** * 向指定URL发送GET方法的请求 * @param url 发送请求的URL * @param corpid 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 * @return URL 所代表远程资源的响应结果 */ public static String sendGet(String url, String corpid,String corpsecret) { String result = ""; BufferedReader in = null; try { String urlNameString = url + "/cgi-bin/gettoken?corpid=" + corpid+"&corpsecret="+corpsecret; URL realUrl = new URL(urlNameString); // 打开和URL之间的连接 HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection(); //URLConnection connection = realUrl.openConnection(); //connection.setReadTimeout(5000); //connection.setConnectTimeout(5000); System.setProperty("sun.net.client.defaultReadTimeout", "30000"); System.setProperty("sun.net.client.defaultConnectTimeout", "30000"); // 设置通用的请求属性 connection.setRequestProperty("Charsert", "UTF-8"); connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); //connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "keep-alive"); //System.setProperty("http.keepAlive", "false"); //connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Firefox/68.0"); // 建立实际的连接 //connection.connect(); // 获取所有响应头字段 Map<String, List<String>> map = connection.getHeaderFields(); // 遍历所有的响应头字段 for (String key : map.keySet()) { System.out.println(key + "--->" + map.get(key)); } // 定义 BufferedReader输入流来读取URL的响应 //connection.getHeaderFields(); //connection.setFollowRedirects(false); System.out.println(connection.getURL()); //System.out.println(connection.getInputStream()); //System.out.println(connection.getResponseCode()); in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = in.readLine()) != null) { result+=line; } connection.disconnect(); } catch (Exception e) { System.out.println("发送GET请求出现异常!" + e); e.printStackTrace(); return "发送GET请求出现异常!" + e; } //使用finally块来关闭输入流 finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result; } /** * 向指定 URL 发送POST方法的请求 * @param url 发送请求的 URL * @param out_str 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 * @return 所代表远程资源的响应结果 */ public static String sendPost(String url,String touser, String content,String access_token) { PrintWriter out = null; BufferedReader in = null; String result = ""; try { String posturl=url+"access_token="+access_token; URL Url = new URL(posturl); // 打开和URL之间的连接 HttpURLConnection conn = (HttpURLConnection) Url.openConnection(); //URLConnection conn = realUrl.openConnection(); conn.setReadTimeout(5000); conn.setConnectTimeout(5000); // 设置通用的请求属性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); //System.setProperty("http.keepAlive", "false"); System.setProperty("https.protocols", "TLSv1"); 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(); // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); // 获取URLConnection对象对应的输出流 JSONObject jsonObject = new JSONObject(); jsonObject.put("touser", touser); jsonObject.put("toparty", ""); jsonObject.put("totag", ""); jsonObject.put("msgtype", "text"); jsonObject.put("agentid", 1000028); JSONObject js=new JSONObject(); js.put("content", content); jsonObject.putOpt("text", js); jsonObject.put("safe", 0); jsonObject.put("enable_id_trans", 0); jsonObject.put("enable_duplicate_check", 0); jsonObject.put("duplicate_check_interval", 1800); //System.out.println(jsonObject); OutputStreamWriter outWriter = new OutputStreamWriter(conn.getOutputStream(), "UTF-8"); out = new PrintWriter(outWriter); // 发送请求参数 out.print(jsonObject); // flush输出流的缓冲 out.flush(); // 定义BufferedReader输入流来读取URL的响应 in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8")); String line; while ((line = in.readLine()) != null) { result+=line; } conn.disconnect(); } catch (Exception e) { System.out.println("发送 POST 请求出现异常!" + e); e.printStackTrace(); return "发送 POST 请求出现异常!" + e; } // 使用finally块来关闭输出流、输入流 finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return result; } public static void main(String[] args) throws JSONException { String geturl="https://qyapi.weixin.qq.com"; String corpid="xxxxxxxxxxxxxxxxx"; String corpsecret="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; JSONObject Result_get = new JSONObject(QyAPI_WeiXin_Send_Message.sendGet(geturl, corpid,corpsecret)); System.out.println(Result_get); String access_token = Result_get.getString("access_token"); String posturl="https://qyapi.weixin.qq.com/cgi-bin/message/send?"; String userid="xxxxxxxxxxxx"; String context="你的快递已到,请携带工卡前往邮件中心领取。\n出发前可查看<a href=\"http://work.weixin.qq.com\">邮件中心视频实况</a>,聪明避开排队。"; String s = QyAPI_WeiXin_Send_Message.sendPost(posturl,userid, context,access_token); System.out.println(s); } }
回答关注问题邀请回答
收藏

2 个回答

  • 企业微信运营专员-千夜
    企业微信运营专员-千夜
    星期一 17:21

    您好,看报错是本地的环境,可以先自行调试下

    星期一 17:21
    有用
    回复
  • bern
    bern
    09-12

    在MyEclipse中运行正常,放到PL/SQL里就这样;

    09-12
    有用
    回复
登录 后发表内容
问题标签