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

187 lines
6.6 KiB
Java
Raw Normal View History

2019-07-01 11:27:00 +08:00
package com.ljsd.util;
2021-11-18 13:30:26 +08:00
import com.alibaba.fastjson.JSONObject;
2019-07-01 11:27:00 +08:00
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;
2022-07-13 11:35:20 +08:00
import org.apache.http.impl.client.DefaultHttpClient;
2019-07-01 11:27:00 +08:00
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
2021-11-18 13:30:26 +08:00
import java.io.*;
2022-07-13 11:35:20 +08:00
import java.net.*;
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();
}
}
2022-07-13 11:35:20 +08:00
public static String doPost2(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));
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
CloseableHttpResponse response2 = httpclient.execute(httpPost);
try {
HttpEntity entity = response2.getEntity();
return EntityUtils.toString(entity);
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
response2.close();
}
}
2019-07-01 11:27:00 +08:00
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
2021-11-18 13:30:26 +08:00
public static String httpGetRequest(String httpUrl){
2021-11-17 18:24:19 +08:00
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;
}
2021-11-18 13:30:26 +08:00
2022-07-13 10:40:00 +08:00
2021-11-18 13:30:26 +08:00
/**
* URL POST
*
* @param url
* URL
* @param param
* name1=value1&name2=value2
* @return
*/
public static String sendPost(String url, JSONObject param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
2022-07-13 10:40:00 +08:00
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
2021-11-18 13:30:26 +08:00
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
2022-07-13 11:35:20 +08:00
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
2021-11-18 13:30:26 +08:00
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
2019-07-01 11:27:00 +08:00
}