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短息发送测试");
}
}
java 用第三方提供的接口实现短信的发送
最新推荐文章于 2021-02-27 23:32:49 发布