日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java短信接口开发完整项目_java项目接入第三方短信接口

發布時間:2023/12/18 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java短信接口开发完整项目_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短信接口开发完整项目_java项目接入第三方短信接口的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。