java项目接入第三方短信接口

1.根据第三方短信接口提供的例子进行参考接入

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;

// 构造单例

    private HttpClientUtil()

{

MultiThreadedHttpConnectionManager httpConnectionManager =new MultiThreadedHttpConnectionManager();

HttpConnectionManagerParams params =new HttpConnectionManagerParams();

// 默认连接超时时间

        params.setConnectionTimeout(60000);

// 默认读取超时时间

        params.setSoTimeout(60000);

// 默认单个host最大连接数

        params.setDefaultMaxConnectionsPerHost(200);// very important!!

        // 最大总连接数

        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;

}

/**

    * 发送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=单发即时短信测试&addserial=10086&sendtime=20090101101010";

// System.out.println(doGetRequest(url));

    }

}

上面的例子用到了单例模式和内部类的方法实现的,

在自己项目的代码中中需要先查询出 上面main方法中url需要的字段类型,然后进行拼接成上面的url,请求这个url就可以了,

请求过后看下后台输出的内容,如果返回值为0,则短信发送成功,下面是我自己的部分代码为例:

public String doNoteBook(HttpServletRequest request, HttpServletResponse response)

{

try

    {

//查询出正在举办活动的本期客户的房间号room为空的电话号码

        List phones =customerMapper.getCustomerPhoneByPeriodsAndRoom();

//String messages = request.getParameter("message");

        String messages ="尊敬的";

String messag =",您的房间号为:";

//取到的中文转码

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);

//可以在此处根据返回值进行判断

System.out.println("*****************="+responseString);

}

}catch (Exception e)

{

e.printStackTrace();

}

return null;

}

经测试有效

你可能感兴趣的:(java项目接入第三方短信接口)