1.post请求
入参类型
application/x-www-form-urlencoded
maven依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.5</version>
</dependency>
创建类HttpSSLUtil
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Map;
import java.util.Objects;
public class HttpSSLUtil {
private static final Logger logger = LoggerFactory.getLogger(HttpSSLUtil.class);
public static String postForForm(String url, Map<String, String> parms) {
HttpPost httpPost = new HttpPost(url);
ArrayList<BasicNameValuePair> list = new ArrayList<>();
parms.forEach((key, value) -> list.add(new BasicNameValuePair(key, value)));
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
if (Objects.nonNull(parms) && parms.size() >0) {
httpPost.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
}
httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
InputStream content = httpPost.getEntity().getContent();
InputStreamReader inputStreamReader = new InputStreamReader(content, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String readLine = bufferedReader.readLine();
String s = URLDecoder.decode(readLine, "UTF-8");
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String result= EntityUtils.toString(entity, "UTF-8");
return result;
}catch (IOException e) {
e.printStackTrace();
}finally {
if (Objects.nonNull(httpClient)){
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}
调用:
Map<String, String> sendMsg = new HashMap<String, String>();
sendMsg.put("type", "1");
sendMsg.put("name", "hello");
String url="https://....";
log.info("http-post-form,发送网址:" + url + "发送参数:" + sendMsg);
String result = HttpSSLUtil.postForForm(url, sendMsg);
log.info("http-post-form,返回结果:" + result);
JSONObject object = JSONObject.parseObject(result);
String code = object.getString("code");
if (!StringUtils.isEmpty(code) && "200" != code) {
//失败处理
}
2.get请求
入参类型
application/x-www-form-urlencoded
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HttpSSLUtil {
private static final Logger logger = LoggerFactory.getLogger(HttpSSLUtil.class);
public static String sendGet(String url, String param, String contentType) {
StringBuilder result = new StringBuilder();
BufferedReader in = null;
try
{
String urlNameString = url + "?" + param;
logger.info("sendGet - {}", urlNameString);
URL realUrl = new URL(urlNameString);
URLConnection connection = realUrl.openConnection();
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.connect();
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), contentType));
String line;
while ((line = in.readLine()) != null)
{
result.append(line);
}
logger.info("recv - {}", result);
}
catch (ConnectException e)
{
logger.error("调用sendGet ConnectException, url=" + url + ",param=" + param, e);
}
catch (SocketTimeoutException e)
{
logger.error("调用sendGet SocketTimeoutException, url=" + url + ",param=" + param, e);
}
catch (IOException e)
{
logger.error("调用sendGet IOException, url=" + url + ",param=" + param, e);
}
catch (Exception e)
{
logger.error("调用sendGet Exception, url=" + url + ",param=" + param, e);
}
finally
{
try
{
if (in != null)
{
in.close();
}
}
catch (Exception ex)
{
logger.error("调用in.close Exception, url=" + url + ",param=" + param, ex);
}
}
return result.toString();
}
}
调用:
String params = "id=" + id + "&token=" + token;
String url = "http://...";
String result = HttpSSLUtil.sendGet(url, params ,"UTF-8");
log.info("https-get,返回结果:" + result);
JSONObject object = JSONObject.parseObject(result);
String error = object.getString("error");