首先 下载 3个jar 包:
程序代码:
package xuyan.com; import java.io.IOException; import java.io.UnsupportedEncodingException; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.PostMethod; public class SendMessage { /** * @param args * @throws IOException * @throws UnsupportedEncodingException */ public static void main(String[] args) throws UnsupportedEncodingException, IOException { // TODO Auto-generated method stub String Mobiltext="您好!你的快递已送达,请于12点前,到石油学院2号门领取,过期不候!"; HttpClient client = new HttpClient(); PostMethod post = new PostMethod("http://gbk.sms.webchinese.cn"); post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gbk");//在头文件中设置转码 NameValuePair[] data ={ new NameValuePair("Uid", "skyxuyan"),new NameValuePair("Key", "3955581ae5bc42ba0e29"),new NameValuePair("smsMob","1804570xxxx"),new NameValuePair("smsText",Mobiltext)}; 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("gbk")); System.out.println(result); post.releaseConnection(); } }
Key需自己到网上申请:
申请具体的Uid ,key 到这里申请:http://sms.webchinese.cn/ (key对应秘钥)