miduo_login/src/main/java/com/ljsd/util/HttpUtils.java

111 lines
3.9 KiB
Java
Raw Normal View History

2019-07-01 11:27:00 +08:00
package com.ljsd.util;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
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;
2021-11-17 18:24:19 +08:00
import java.io.BufferedReader;
2019-07-01 11:27:00 +08:00
import java.io.IOException;
2021-11-17 18:24:19 +08:00
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
2019-07-01 11:27:00 +08:00
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class HttpUtils {
public static String doPost(String url, Map<String,String> parms) throws IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> nvps =getFromMap(parms);
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response2 = httpclient.execute(httpPost);
try {
HttpEntity entity = response2.getEntity();
return EntityUtils.toString(entity);
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
response2.close();
}
}
public static List<NameValuePair> getFromMap(Map<String,String> parms){
List<NameValuePair> nvps = new ArrayList<NameValuePair>(parms.size());
for(Map.Entry<String,String> item : parms.entrySet()){
nvps.add(new BasicNameValuePair(item.getKey(), item.getValue()));
}
return nvps;
}
2021-11-17 18:24:19 +08:00
public static String httpRequest(String httpUrl){
HttpURLConnection connection = null;
InputStream is = null;
BufferedReader br = null;
String result = null;// 返回结果字符串
try {
// 创建远程url连接对象
URL url = new URL(httpUrl);
// 通过远程url连接对象打开一个连接强转成httpURLConnection类
connection = (HttpURLConnection) url.openConnection();
// 设置连接方式get
connection.setRequestMethod("GET");
// 设置连接主机服务器的超时时间15000毫秒
connection.setConnectTimeout(15000);
// 设置读取远程返回的数据时间60000毫秒
connection.setReadTimeout(60000);
// 发送请求
connection.connect();
// 通过connection连接获取输入流
if (connection.getResponseCode() == 200) {
is = connection.getInputStream();
// 封装输入流is并指定字符集
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
// 存放数据
StringBuffer sbf = new StringBuffer();
String temp = null;
while ((temp = br.readLine()) != null) {
sbf.append(temp);
sbf.append("\r\n");
}
result = sbf.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭资源
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
connection.disconnect();// 关闭远程连接
}
return result;
}
2019-07-01 11:27:00 +08:00
}