文档概览

JAVA

/**
 * @Title: HttpDemo4HttpClient.java
 * @Package cn.zd.http
 * @Description: TODO
 * Copyright: Copyright (c) 2015
 * Company: 上海智鼎信息技术有限公司
 *
 * @author Mark
 * @date 2015-9-6 上午11:58:29
 * @version V1.0
 */
package com.zd.demo.test;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
 
 
/**
 * Http Demo for Java
 * 参考文档《短信综合信息管理系统_Http接口文档.doc》
 */
public class HttpDemo4HttpClient {
 
//帐号
static String account = "";//
//密码
static String password = "";//
//校验码
static String veryCode = "";
 
final static String HTTP_URL = "http://120.27.149.106:8030";
 
 
/**
* 模版短信,无需人工审核,直接发送
*   (短信模版的创建参考客户端操作手册)
*   模版:@1@会员,动态验证码@2@(五分钟内有效)。请注意保密,勿将验证码告知他人。
*   参数值:@1@=member,@2@=4293
*   最终短信内容:【短信签名】member会员,动态验证码4293(五分钟内有效)。请注意保密,勿将验证码告知他人。
*   
*   提交路径:
*   UTF-8编码:/service/httpService/httpInterface.do?method=sendUtf8Msg
*   GBK编码:/service/httpService/httpInterface.do?method=sendGbkMsg
* @param mobile 手机号码
* @param tempId 模版编号,对应客户端模版编号
* @param content 各参数值,以英文逗号隔开,如:@1@=member,@2@=4293
* @return
* @throws Exception
*/
public static String sendTemplateSms(String mobile,String tempId,String content) throws Exception{
String address = HTTP_URL + "/service/httpService/httpInterface.do?method=sendUtf8Msg";
 
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("username", account));
nvps.add(new BasicNameValuePair("password", password));
nvps.add(new BasicNameValuePair("veryCode", veryCode));
nvps.add(new BasicNameValuePair("mobile", mobile));
nvps.add(new BasicNameValuePair("content", content));
nvps.add(new BasicNameValuePair("msgtype", "2"));
nvps.add(new BasicNameValuePair("tempid", tempId));
nvps.add(new BasicNameValuePair("code", "utf-8"));
 
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(address);
try {
httpPost.setEntity(new UrlEncodedFormEntity(nvps,Constants.CHARSET_UTF8));//, Constants.ZST_CHARSET
HttpResponse resp = httpClient.execute(httpPost);
if (resp.getStatusLine().getStatusCode() == Constants.HTTP_STATUS_CODE_SUCCESS) {// 请求成功
String responseXmlStr = null;// 请求对方响应内容(XML格式字符串)
HttpEntity entity = resp.getEntity();
responseXmlStr = EntityUtils.toString(entity,Constants.CHARSET_UTF8).trim();
EntityUtils.consume(entity);
System.out.println("===============>>>>>responseXmlStr:="+responseXmlStr);
return responseXmlStr;
}else{
System.out.println("----------------Status not 200------>" + resp.getStatusLine().getStatusCode());
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
 
} finally {
 
}
return "";
}
 
/**
* 发送普通短信  普通短信发送需要人工审核
* 提交路径:
*   UTF-8编码:/service/httpService/httpInterface.do?method=sendUtf8Msg
*   GBK编码:/service/httpService/httpInterface.do?method=sendGbkMsg
* @param mobile 手机号码, 多个号码以英文逗号隔开,最多支持100个号码
* @param content 短信内容 
* @return 
* @throws Exception
*/
public static String sendSms(String mobile,String content) throws Exception{
String address = HTTP_URL + "/service/httpService/httpInterface.do?method=sendUtf8Msg";
 
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("username", account));
nvps.add(new BasicNameValuePair("password", password));
nvps.add(new BasicNameValuePair("veryCode", veryCode));
nvps.add(new BasicNameValuePair("mobile", mobile));
nvps.add(new BasicNameValuePair("content", content));
nvps.add(new BasicNameValuePair("msgtype", "1"));
nvps.add(new BasicNameValuePair("code", "utf-8"));
 
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(address);
try {
httpPost.setEntity(new UrlEncodedFormEntity(nvps,Constants.CHARSET_UTF8));//, Constants.ZST_CHARSET
HttpResponse resp = httpClient.execute(httpPost);
if (resp.getStatusLine().getStatusCode() == Constants.HTTP_STATUS_CODE_SUCCESS) {// 请求成功
String responseXmlStr = null;// 请求对方响应内容(XML格式字符串)
HttpEntity entity = resp.getEntity();
responseXmlStr = EntityUtils.toString(entity,Constants.CHARSET_UTF8).trim();
EntityUtils.consume(entity);
System.out.println("===============>>>>>responseXmlStr:="+responseXmlStr);
return responseXmlStr;
}else{
System.out.println("----------------Status not 200------>" + resp.getStatusLine().getStatusCode());
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
 
} finally {
 
}
return "";
}
/**
* @param args
* void 
* @exception
*/
public static void main(String[] args) throws Exception{
//普通短信
// String result = sendSms("15618918208", " 上海智鼎信息技术有限公司");
 
//模板短信
//String result = sendTemplateSms("15618918208", "ZD10003-0010", "@1@=1627");
 
//System.out.println(result);
}
 
}
 
 
QQ在线咨询
服务热线
021-64360230
24小时电话
15900894283