java 短信发送例子 2

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 短信发送例子 2相关的知识,希望对你有一定的参考价值。

package com.google;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;

public class SendSMS {
 public static void main(String[] args) {
  String url = "http://finance.aliyun.com/recharge/sendMessage.json";
  String token = "9ZuD6iN0aFDN2SzFjEMKuA";
  String mobile = "13800138000";
  String sendStr = "奇瑞丰 草根银行 8888 8888 8888 888";
  SendSMS ss = new SendSMS();
  String result = ss.getPostPage(url, mobile, sendStr, token);
  System.out.println(result);
 }

 public String getPostPage(String url, String mobile, String sendStr,
   String _csrf_token) {
  HttpClient client = new HttpClient();
  PostMethod method = new PostMethod(url);
  method.getParams().setContentCharset("utf-8");
  method.setRequestHeader("Accept",
    "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
  method.setRequestHeader("Accept-Encoding", "gzip,deflate,sdch");
  method.setRequestHeader("Accept-Language", "zh-CN,zh;q=0.8");
  method.setRequestHeader("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3");
  method.setRequestHeader("Host", "finance.aliyun.com");
  method.setRequestHeader("Origin", "http://finance.aliyun.com");
  method.setRequestHeader("Connection", "Keep-Alive");
  method.setRequestHeader(
    "Referer",
    "http://finance.aliyun.com/account/recharge.htm?spm=0.0.0.0.elRHXV_0.0.0.0.uWxMEt");
  method.setRequestHeader(
    "User-Agent",
    "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7");

  method.setRequestHeader(
    "Cookie",
    "JSESSIONID=UM5663B1-YORLH533CMAAD3D7NVG72-IDQ1HNPH-HA8; www_aliyun=5ugne0vg8ng4s69bgnbc8c1j23; login_aliyunid="[email protected]"; login_aliyunid_ticket=qIZCr6t7SGxRigm2Cb4fGaCdBZWIzmgdHq6sXXZQg4KFWufyvpeV*0*Cm58slMT1tJw3_l$$TPk34m6UmF8qM3z_ar76cgYPs0P6UIMBujw6KvAsYKof_BNpwU_TOTNChZBoeM1KJexdfb9zhYnsN5so0; login_aliyunid_csrf=_csrf_tk_1815088030290190; hssid=1WRqTEJuUmJU3dixAG_C7qQ1; hsite=6; hsts=1388030291344; cfp0=5FwuPCGHOOv2eKqzpHY5awfZXDm8gZY9mmWXuXkMRMdFqGPmDjaZaUGN%2B8VacgamIUkNOXJVj2q8ZbfLiSga1Wn0H0Ewtw8elLKTPS4qgsKFAfS88fki2CU5U7c9Cg9hvg9Uup%2BHkY8uoegROllFuoVTuLXJBuysWEDQA1p%2FY%2BLPNa5XWKRQzESsQs6WvspH9859ZPLFhpzLTQ%2B%2FY%2B2j24QJKF4VqYSKmMpjWW3EDwd2hKsO1xhbDu78Aotv10GDS6Amw3BOrXB%2FUImAwXwx%2FGZVqDlf0DjWg8DXoOPg1pIv5iSw9QxPYLLTdFF%2FS3lgiCJWzQGShIGfKpFHS21xaNCX0sqhhy%2BXKBNrD4z5X%2FaoyFYvdESFE9GuopYC4CAGQmFZ5CNMNI7Kes4tNvgc%2Bg%3D%3D; _ga=GA1.2.1860272764.1387960845");
  method.setRequestHeader("X-Requested-With", "XMLHttpRequest");
  // 设置请求参数

  method.addParameter("mobile", mobile);
  method.addParameter("sendStr", sendStr);
  method.addParameter("_csrf_token", _csrf_token);

  String responseStr = "";
  try {
   client.executeMethod(method);
   // responseStr = method.getResponseBodyAsString();
   InputStream resStream = method.getResponseBodyAsStream();
   BufferedReader br = new BufferedReader(new InputStreamReader(
     resStream));
   StringBuffer resBuffer = new StringBuffer();
   String resTemp = "";
   while ((resTemp = br.readLine()) != null) {
    resBuffer.append(resTemp);
   }
   responseStr = resBuffer.toString();
   method.releaseConnection();

  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

  return responseStr;

 }
}

 

 

 

请求之后,成功返回值:{"messageId":107913122600012591,"success":true,"tips":"200successful"}

如果没有返回,则表示发送失败,可能是session有问题。

以上是关于java 短信发送例子 2的主要内容,如果未能解决你的问题,请参考以下文章

三个例子 —JAVA发送http get/post请求,调用http接口方法

java网络编程实现客户端连接服务器端,并发送消息例子。(阻塞式的方法)

java对接阿里云短信服务器,附代码和截图

[Java] JavaMail 发送 html 格式带附件的邮件

刚学习netty和java,不会用,但现在急需,您有没有用netty编写的服务器和客户端的例子,要能互相发送包

java中如何用json格式发送并接受arrayList?