1.根據(jù)第三方短信接口提供的例子進行參考接入
package com.jc.cus.utils;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import java.io.*;
public class HttpClientUtil
{
private static HttpClientclient =null;
// 構(gòu)造單例
? ? private HttpClientUtil()
{
MultiThreadedHttpConnectionManager httpConnectionManager =new MultiThreadedHttpConnectionManager();
HttpConnectionManagerParams params =new HttpConnectionManagerParams();
// 默認連接超時時間
? ? ? ? params.setConnectionTimeout(60000);
// 默認讀取超時時間
? ? ? ? params.setSoTimeout(60000);
// 默認單個host最大連接數(shù)
? ? ? ? params.setDefaultMaxConnectionsPerHost(200);// very important!!
? ? ? ? // 最大總連接數(shù)
? ? ? ? params.setMaxTotalConnections(500);// very important!!
? ? ? ? httpConnectionManager.setParams(params);
client =new HttpClient(httpConnectionManager);
client.getParams().setConnectionManagerTimeout(3000);
// client.getParams().setIntParameter("http.socket.timeout", 10000);
// client.getParams().setIntParameter("http.connection.timeout", 5000);
? ? }
private static class ClientUtilInstance
{
private static final HttpClientUtilClientUtil =new HttpClientUtil();
}
public static HttpClientUtil getInstance()
{
return ClientUtilInstance.ClientUtil;
}
/**
? ? * 發(fā)送http GET請求靠瞎,并返回http響應字符串
? ? *
? ? * @param urlstr
? ? *? ? ? ? ? ? 完整的請求url字符串
? ? * @return
? ? */
? ? public String doGetRequest(String urlstr) {
String response ="";
HttpMethod httpmethod =new GetMethod(urlstr);
try {
int statusCode =client.executeMethod(httpmethod);
InputStream _InputStream =null;
if (statusCode == HttpStatus.SC_OK) {
_InputStream = httpmethod.getResponseBodyAsStream();
}
if (_InputStream !=null) {
response = GetResponseString(_InputStream,"UTF-8");
}
}catch (HttpException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally {
httpmethod.releaseConnection();
}
return response;
}
public String doPostRequest(String postUrl) {
String response ="";
PostMethod postMethod =new PostMethod(postUrl);
try {
int statusCode =client.executeMethod(postMethod);
if (statusCode == HttpStatus.SC_OK) {
InputStream _InputStream =null;
if (statusCode == HttpStatus.SC_OK) {
_InputStream = postMethod.getResponseBodyAsStream();
}
if (_InputStream !=null) {
response = GetResponseString(_InputStream,"UTF-8");
}
}
}catch (HttpException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally {
postMethod.releaseConnection();
}
return response;
}
/**
*
? ? * @param _InputStream
? ? * @param Charset
? ? * @return
? ? */
? ? public String GetResponseString(InputStream _InputStream, String Charset) {
String response ="";
try {
if (_InputStream !=null) {
StringBuffer buffer =new StringBuffer();
InputStreamReader isr =new InputStreamReader(_InputStream, Charset);
Reader in =new BufferedReader(isr);
int ch;
while ((ch = in.read()) > -1) {
buffer.append((char) ch);
}
response = buffer.toString();
buffer =null;
}
}catch (Exception e) {
response = response + e.getMessage();
e.printStackTrace();
}
return response;
}
public static void main(String[] args) {
String url ="http://sdk4rptws.eucp.b2m.cn:8080/sdkproxy/sendtimesms.action?cdkey=2SDK-EMY-6688-AAAAA&password=******&phone=1333333333,13444444444&message=單發(fā)即時短信測試&addserial=10086&sendtime=20090101101010";
// System.out.println(doGetRequest(url));
? ? }
}
上面的例子用到了單例模式和內(nèi)部類的方法實現(xiàn)的屑埋,
在自己項目的代碼中中需要先查詢出 上面main方法中url需要的字段類型,然后進行拼接成上面的url忠蝗,請求這個url就可以了歹颓,
請求過后看下后臺輸出的內(nèi)容,如果返回值為0颖医,則短信發(fā)送成功庐完,下面是我自己的部分代碼為例:
public String doNoteBook(HttpServletRequest request, HttpServletResponse response)
{
try
? ? {
//查詢出正在舉辦活動的本期客戶的房間號room為空的電話號碼
? ? ? ? List phones =customerMapper.getCustomerPhoneByPeriodsAndRoom();
//String messages = request.getParameter("message");
? ? ? ? String messages ="尊敬的";
String messag =",您的房間號為:";
//取到的中文轉(zhuǎn)碼
messages = URLEncoder.encode(messages,"utf-8");
messag = URLEncoder.encode(messag,"utf-8");
for (PhoneVO phone: phones)
{
String customerName = phone.getName();
String name = URLEncoder.encode(customerName,"utf-8");
String phonenum ="phone="+phone.getPhone();
String message ="message="+messages+name+messag+phone.getRoom();
String url =notePaths+cdkey+"&"+password+"&"+phonenum+"&"+message;
//System.out.println("*****************="+url);
? ? ? ? ? ? String responseString = HttpClientUtil.getInstance().doGetRequest(url);
//可以在此處根據(jù)返回值進行判斷
System.out.println("*****************="+responseString);
}
}catch (Exception e)
{
e.printStackTrace();
}
return null;
}
經(jīng)測試有效