概述
今天在项目中接到一个小任务要对接第三方短信平台开发,这个之前就写过,正在想拿之前的代码来看时,发现找不到了,索性果断写了一个,并在此记录下来。
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
/**
* Http测试 2016/12/06
* @author pwl
*
*/
public class TestHttpClient {
public static void main(String[] args) {
doPost();
}
public static void doPost(){
String url = "http://test:80/ApiService.asmx/Send?";
String account = "test";
String pwd = "test";
String product = "test";
String phoneNumber = "test";
String content = "测试短信内容";
StringBuilder builder = new StringBuilder(url);
builder.append("account="+account + "&");//添加账号
builder.append("pwd="+pwd + "&");//添加密码
builder.append("product="+product + "&");//添加产品号
builder.append("mobile="+phoneNumber +"&");//添加手机号码
builder.append("message="+content);
URL localURL = null;
URLConnection connection = null;
HttpURLConnection httpURLConnection = null;
InputStream inputStream = null;
try {
localURL = new URL(builder.toString());
connection = localURL.openConnection();
httpURLConnection = (HttpURLConnection)connection;
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("GET");
String result = null;
inputStream = httpURLConnection.getInputStream();
result = convertStreamToString(inputStream);
String resultCode = parseResult(result);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 拼接请求参数
* @param parameterMap
* @return
*/
public static String translateParams(Map parameterMap){
StringBuffer parameterBuffer = new StringBuffer();
if (parameterMap != null) {
Iterator iterator = parameterMap.keySet().iterator();
String key = null;
String value = null;
while (iterator.hasNext()) {
key = (String)iterator.next();
if (parameterMap.get(key) != null) {
value = (String)parameterMap.get(key);
} else {
value = "";
}
parameterBuffer.append(key).append("=").append(value);
if (iterator.hasNext()) {
parameterBuffer.append("&");
}
}
}
return parameterBuffer.toString();
}
/**
* 将结果流转为字符串
* @param is
* @return
*/
public static String convertStreamToString(InputStream is) {
StringBuilder builder = new StringBuilder();
byte[] bytes = new byte[4096];
int size = 0;
try {
while ((size = is.read(bytes)) > 0) {
String str = new String(bytes, 0, size, "UTF-8");
builder.append(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return builder.toString();
}
/**
* 解析返回结果
*/
public static String parseResult(String xml){
String resultCode = null;
try {
Document document = DocumentHelper.parseText(xml);
Element root = document.getRootElement();
String result = root.getStringValue();
if(result.contains(",")){
resultCode = result.split(",")[0];
}
} catch (DocumentException e) {
e.printStackTrace();
}
return resultCode;
}
}
因为短信接口返回的是一个xml的字符串,具体内容如下:
-102,2016/12/7 9:16:44
在这里使用Dom4j 进行解析,解析方法parseResult,而translateParams方法是将Map中的参数进行拼接,一个工具方法。
最后
以上就是落寞小蘑菇为你收集整理的java 短信平台接口_Java 通过Http请求对接第三方短信平台接口的全部内容,希望文章能够帮你解决java 短信平台接口_Java 通过Http请求对接第三方短信平台接口所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复