您的位置:首页 > 编程语言 > Java开发

java 调用鹏讯通短信平台api发送短信

2014-03-19 14:11 351 查看
鹏讯通短信平台发送短信实例代码如下:

依赖jar:httpcore-4.1.4.jar,httpclient-4.1.3.jar,httpclient-cache-4.1.3.jar,httpmime-4.1.3.jar
package com.test.yckj;

import java.io.IOException;

import java.io.InputStream;

import java.io.UnsupportedEncodingException;

import java.util.ArrayList;

import java.util.List;

import org.apache.commons.io.IOUtils;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.NameValuePair;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.message.BasicNameValuePair;

public class SendMassageUtil {

String url = "http://www.sms8810086.com/jk.aspx";

//String url = "http://www.sms8810086.com";

//发送账号

String zh = "ceshi01";

//发送密码

String mm = "123";

//用户通道

String sms_type = "40";

/**

* @param nr 短信内容

* @param hm 全部被叫号码

* @return

*/

public String sendSms(String nr, String hm) {

String result = null;

DefaultHttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost(url);

List<NameValuePair> params = new ArrayList<NameValuePair>();

params.add(new BasicNameValuePair("zh", zh)); //用户名称

params.add(new BasicNameValuePair("mm", mm)); //密码

params.add(new BasicNameValuePair("sms_type", sms_type)); //用户通道

params.add(new BasicNameValuePair("hm", hm)); //接受人电话号码,如果有多个以逗号隔开

params.add(new BasicNameValuePair("nr", nr)); //短信内容

try {

httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

HttpResponse response = httpclient.execute(httppost);

HttpEntity entity = response.getEntity();

if (entity != null) {

InputStream instream = null;

try {

instream = entity.getContent();

result = IOUtils.toString(instream, "utf-8");

} finally {

if (instream != null)

instream.close();

}

}

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return result;

}

/**

* @param args

*/

public static void main(String[] args) {

SendMassageUtil api = new SendMassageUtil();

String nr = "鹏讯通短信平台";

String hm = "135……"; //电话号码

System.out.println("号码:" + hm + " 内容:" + nr);

System.out.println("结果:" + api.sendSms(nr, hm));

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: