java 實現 短信發送案例1

案例是使用《 使用中國網建提供的SMS短信平臺》進行發送的。地址:http://sms.webchinese.cn/api.shtmlhtml

1. 首先到 該地址註冊一個用戶java

2. 進行密鑰獲取web

3.設置短信簽名apache

代碼:如下是用utf-8接口實現api

import java.io.IOException;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

public class Test {

public static void main(String[] args) throws HttpException, IOException {
            HttpClient client = new HttpClient();
            PostMethod post = new PostMethod("http://utf8.sms.webchinese.cn" );
             post.addRequestHeader( "Content-Type", "application/x-www-form-urlencoded;charset=utf-8" );//在頭文件中設置轉碼
            NameValuePair[] data ={ new NameValuePair( "Uid", "用戶名"),new NameValuePair("Key" , "密鑰" ),new NameValuePair("smsMob","186957XXX,15958XXX,58748XXX,..." ),new NameValuePair("smsText","短信內容")};
             post.setRequestBody( data);

             client.executeMethod( post);
            Header[] headers = post.getResponseHeaders();
             int statusCode = post.getStatusCode();
            System. out.println( "statusCode:"+ statusCode);
             for(Header h : headers)
            {
            System. out.println( h.toString());
            }
            String result = new String(post.getResponseBodyAsString().getBytes( "utf-8"));
            System. out.println( result); //打印返回消息狀態
             post.releaseConnection();

      }
}

控制檯:app

statusCode:200
Cache-Control: no-cache
Content-Length: 1
Content-Type: text/html; Charset=UTF-8
Expires: Thu, 26 Nov 2015 09:38:22 GMT
Server: Microsoft-IIS/7.5
Set-Cookie: CHNET=Temp%5Fusername=2015112717382399120; expires=Thu, 23-Aug-2018 09:38:22 GMT; path=/
Set-Cookie: ASPSESSIONIDCASDBQTT=EKEJENKDDMHHDFCEMOEGGDPD; path=/
X-Powered-By: ASP.NET
Date: Fri, 27 Nov 2015 09:38:23 GMT
1post

相關文章
相關標籤/搜索