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 Jsch SFTP 递归下载文件夹
    spring-jms,spring-boot-starter-activemq JmsTemplate 发送方式
    Spring Boot 入门之消息中间件篇(转发)
    Springboot websocket使用
    FinalCutPro快捷键
    基本CSS布局三
    As Simple as One and Two
    Game of Credit Cards
    WOW Factor
    Lose it!
  • 原文地址:https://www.cnblogs.com/rchao/p/4549258.html
Copyright © 2011-2022 走看看