//通过httpclient调用接口,传参给客户手机发过去
String jsonParams = "{\"secret\":\""+加密的参数+"\",\"source\":\"固定参数标识\"}";
String url="访问短信接口的地址";
HttpGetWithEntity e = new HttpGetWithEntity(url);//放置URL
e.setHeader("Content-Type", "application/json");
if (!StringUtils.isEmpty(jsonParams)) {
StringEntity se = new StringEntity(jsonParams);//放置参数
e.setEntity(se);
}
//创建Httpclient对象
org.apache.http.client.HttpClient httpclient =new DefaultHttpClient();
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
HttpResponse response = httpclient.execute(e);// 执行http get请求
if (response.getStatusLine().getStatusCode() == 200) {// 判断返回状态是否为200
String content = EntityUtils.toString(response.getEntity(), "utf8");
JSONObject obj=JSONObject.parseObject(content);
String errorMsg=String.valueOf(obj.get("errorMsg"));
if(errorMsg.equals("success")){
return new FebsResponse().message("发送短信成功!");
}else{
return new FebsResponse().message("发送短信失败!");
}
} else {
throw new Exception("获取短信接口,传参发送验证码,请求失败!");
}
HttpGetWithEntity这个class文件的内容是:
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import java.net.URI;
public class HttpGetWithEntity extends HttpEntityEnclosingRequestBase {
private final static String METHOD_NAME = "GET";
@Override
public String getMethod() {
return METHOD_NAME;
}
public HttpGetWithEntity() {
super();
}
public HttpGetWithEntity(final URI uri) {
super();
setURI(uri);
}
HttpGetWithEntity(final String uri) {
super();
setURI(URI.create(uri));
}
}
|