• Java http方式提交短信到短信网关


    URL url = new URL("网关url");//使用post方式,这里不要带参数
    HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
    httpCon.setConnectTimeout(5 * 1000);
    // 设置连接超时时间
    httpCon.setReadTimeout(30 * 1000);
    //设置从主机读取数据超时(单位:毫秒)
    httpCon.setDoOutput(true);
    httpCon.setDoInput(true);
    httpCon.setUseCaches(false);
    httpCon.setRequestMethod("POST");
    httpCon.setInstanceFollowRedirects(true);
    httpCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                
    String info = "发送的消息内容";
    String encodeInfo = URLEncoder.encode(info, "UTF-8");
    String phone = "15966378560";
    //String phone = "15966378560,15966378560";//一般网关接口多号码发送都是用','隔开的;按网关开发文档修改
    
    //请求的参数,根据网关开发文档来组装 
    String content = "user=user&pwd=pwd&tel="+phone+"&info="+encodeInfo;
                       
    byte[] postData = content.getBytes("UTF-8");
    httpCon.setRequestProperty("Content-Length", String.valueOf(postData.length));
    httpCon.connect();
    DataOutputStream out = new DataOutputStream(httpCon.getOutputStream());
    out.write(postData, 0, postData.length);
    out.flush();
    out.close();
    
    int responseCode = httpCon.getResponseCode();
    BufferedReader reader = new BufferedReader(new InputStreamReader(httpCon.getInputStream()));
    while ((line = reader.readLine()) != null) {
         System.out.println(line);
    }
    reader.close();
    String sRet = httpCon.getResponseMessage();
    System.out.println("发送状态:" + sRet);
    httpCon.disconnect();
  • 相关阅读:
    泛型类,泛型方法的使用
    Mapper注解与MapperScan注解
    Configuration注解
    LA 4254 Processor (二分 + 贪心)
    UVa 10382 Watering Grass (贪心 区间覆盖)
    UVA 10795 A Different Task (递归)
    LA 3401 Colored Cubes (搜索 + 暴力)
    uva11464 Even Parity (枚举+递推)
    icpc2021 昆明 K (dfs爆搜)
    hdu3533 (bfs + 模拟)
  • 原文地址:https://www.cnblogs.com/rchao/p/4549258.html
Copyright © 2020-2023  润新知