HttpClientUtil工具类调用短信接口
- 调用短信接口
String url = "xxxxxxxxxxxxxxxxxxxx";
Map<String,String> params = new HashMap<>();
params.put("account","7552");
params.put("mobile",model.getRisenusPhone());
params.put("text", "您的验证码是" + key);
params.put("sign","M2NlOGQ0MzBhYjFlOTBlZjcwOWJkN2Y1NzhhNWUwMTE=");
params.put("extend","");
try {
ArrayList<String> resultParams = new ArrayList<>();
resultParams.add("code");
resultMap = HttpClientUtil.invokeHttp(url, HttpClientUtil.HTTP_POST, params,resultParams);
} catch (IOException e) {
e.printStackTrace();
}
- HttpClientUtil工具类
package com.hw.util;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class HttpClientUtil {
public static String HTTP_GET = "GET";
public static String HTTP_POST = "POST";
public static Map<String, String> invokeHttp(String url,
String method,
Map<String, String> paramMap,
List<String> returnParamList) throws UnsupportedOperationException, IOException {
CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse response = null;
if (StringUtils.equalsIgnoreCase(method, HTTP_GET)) {
StringBuilder urlSb = new StringBuilder(url);
int paramIndex = 0;
for (Map.Entry<String, String> entry : paramMap.entrySet()) {
if (paramIndex == 0) {
urlSb.append("?");
}
urlSb.append(entry.getKey() + "=" + entry.getValue() + "&");
}
urlSb.delete(urlSb.length() - 1, urlSb.length());
HttpGet get = new HttpGet(urlSb.toString());
response = client.execute(get);
}else if (StringUtils.equalsIgnoreCase(method, HTTP_POST)) {
HttpPost post = new HttpPost(url);
String s = JSONObject.toJSON(paramMap).toString();
StringEntity paramEntity = new StringEntity(s,"UTF-8");
paramEntity.setContentEncoding("UTF-8");
post.setHeader("Content-type", "application/json");
post.setEntity(paramEntity);
response = client.execute(post);
}else {
throw new RuntimeException("对不起,该请求方式不支持!");
}
InputStream is = response.getEntity().getContent();
Map<String, String> returnMap = new LinkedHashMap<>();
String ret = IOUtils.toString(is, "UTF-8");
returnMap = (Map<String, String>) JSONObject.parse(ret);
return returnMap;
}
public static String invokeHttp1(String url, String method, Map<String, Object> paramMap, List<String> returnParamList) throws UnsupportedOperationException, IOException {
CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse response = null;
if (StringUtils.equalsIgnoreCase(method, HTTP_GET)) {
StringBuilder urlSb = new StringBuilder(url);
int paramIndex = 0;
for (Map.Entry<String, Object> entry : paramMap.entrySet()) {
if (paramIndex == 0) {
urlSb.append("?");
}
urlSb.append(entry.getKey() + "=" + entry.getValue() + "&");
}
urlSb.delete(urlSb.length() - 1, urlSb.length());
HttpGet get = new HttpGet(urlSb.toString());
response = client.execute(get);
}else if (StringUtils.equalsIgnoreCase(method, HTTP_POST)) {
HttpPost post = new HttpPost(url);
String s = JSONObject.toJSON(paramMap).toString();
System.out.println(s);
StringEntity paramEntity = new StringEntity(s,"UTF-8");
paramEntity.setContentEncoding("UTF-8");
post.setHeader("Content-type", "application/json");
post.setEntity(paramEntity);
response = client.execute(post);
}else {
throw new RuntimeException("对不起,该请求方式不支持!");
}
InputStream is = response.getEntity().getContent();
String result = IOUtils.toString(is, "UTF-8");
return result;
}
}
注意对应引入的坐标
- fastjson包
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.39</version>
</dependency>
- commons.io包
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
- commons.lang3包
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
- http.client包
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.4</version>
</dependency>