java 用第三方提供的接口实现短信的发送

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
* 手机短息发送
* @author zhchen
*
*/
public class PhoneMessageSendUtil {

private static HttpURLConnection httpURLConnection = null;

//用户名
private static String SMSServerUId = "xx";

//密码
private static String SMSServerPassword = "xx";

//第三方提供的接口地址
private static String url = "xxx";

/**
* POST 方式发送消息
*
* @param mobile 手机号
* @param msg 短息内容
* @return
*/
public static String sendPost(String mobile,String msg) {
return sendPost(url , mobile, msg);
}

/**
* GET 方式发送消息
* @param url
* @return
*/
public static String sendGet(String url) {
try {
creatConnection(url);
httpURLConnection.setRequestMethod("GET");
return receiveMessage(httpURLConnection);
} catch (IOException io) {
//logger.log("http close"+io);
} finally {
closeConnection();
}
return null;
}

/**
* POST 方式发送消息
*
* @param url 第三方提供接口的地址
* @param mobile 手机号
* @param msg 短息内容
* @return
*/
public static String sendPost(String url,String mobile,String msg) {
try {
creatConnection(url);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
String urlMsg = "uid="+SMSServerUId+"&pwd="+SMSServerPassword+"&mobile="+mobile+"&msg="+msg;
httpURLConnection.getOutputStream().write(urlMsg.getBytes());
httpURLConnection.getOutputStream().flush();
httpURLConnection.getOutputStream().close();
return receiveMessage(httpURLConnection);
} catch (IOException io) {
//logger.log("http close"+io);
} finally {
closeConnection();
}
return null;
}

/**
* 创建于第三方的链接
* @param url
*/
private static void creatConnection(String url) {
try {
if (httpURLConnection != null)
httpURLConnection.disconnect();

httpURLConnection = ((HttpURLConnection) new URL(url)
.openConnection());
// httpURLConnection.setRequestProperty("Content-Type", "text/html;charset=gbk");
} catch (IOException io) {
io.printStackTrace();
//logger.log("Http Connect to :" + url + " " + "IOFail!");
} catch (Exception ex) {
//logger.log("Http Connect to :" + url + " " + "Failed" + ex);
}
}

/**
* 关闭链接
*/
private static void closeConnection() {
try {
if (httpURLConnection != null)
httpURLConnection.disconnect();
} catch (Exception ex) {

}
}

/**
* 操作产生的结果
* @param httpURLConnection
* @return
*/
private static String receiveMessage(HttpURLConnection httpURLConnection) {
String responseBody = null;
try {

InputStream httpIn = httpURLConnection.getInputStream();

if (httpIn != null) {

ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
byte tempByte;
while (-1 != (tempByte = (byte) httpIn.read()))
byteOut.write(tempByte);
responseBody = new String(byteOut.toByteArray(), "gbk");
if(responseBody != null || responseBody.trim().length() != 0){
responseBody = responseBody.substring(1);
}
System.out.println(responseBody);
}
} catch (IOException ioe) {
/*logger.log("Http Connect tosss :" + ioe.getLocalizedMessage() + " "
+ "IOEFail!");*/
return null;
}
return responseBody;
}

public static void main(String[] args) {
for(int i=0;i<10;i++)
sendPost("1","java短息发送测试");
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值