zoukankan      html  css  js  c++  java
  • 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();
  • 相关阅读:
    每日扫盲(四):java之Netty原理和使用
    每日扫盲(三):id_rsa、id_rsa.pub 、authorized_keys
    每日扫盲(二):xxx.dll文件的作用
    每日扫盲(一):java的rmi
    工具列表
    hadoop学习笔记(七):hadoop2.x的高可用HA(high avaliable)和联邦F(Federation)
    window.history对象
    window.location对象
    推送本地文件夹到github
    docker的使用01
  • 原文地址:https://www.cnblogs.com/rchao/p/4549258.html
Copyright © 2011-2022 走看看