请书写一个java http和https get post 调用的工具类
时间: 2023-10-28 20:04:34 浏览: 57
以下是一个简单的Java HTTP/HTTPS GET和POST调用的工具类:
```java
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
public class HttpUtils {
private final static String USER_AGENT = "Mozilla/5.0";
// HTTP GET请求
public static String sendGet(String url) throws Exception {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// 默认值我GET
con.setRequestMethod("GET");
// 添加请求头
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// 打印响应结果
System.out.println(response.toString());
return response.toString();
}
// HTTP POST请求
public static String sendPost(String url, Map<String, String> params) throws Exception {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// 添加请求头
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
// 发送POST请求必须设置如下两行
con.setDoOutput(true);
con.setDoInput(true);
// 构建请求参数
StringBuilder sb = new StringBuilder();
for (String key : params.keySet()) {
sb.append(key).append("=").append(params.get(key)).append("&");
}
String urlParameters = sb.toString();
urlParameters = urlParameters.substring(0, urlParameters.length() - 1);
// 发送请求参数
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// 打印响应结果
System.out.println(response.toString());
return response.toString();
}
// HTTPS GET请求
public static String sendSecureGet(String url) throws Exception {
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
// 默认值我GET
con.setRequestMethod("GET");
// 添加请求头
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// 打印响应结果
System.out.println(response.toString());
return response.toString();
}
// HTTPS POST请求
public static String sendSecurePost(String url, Map<String, String> params) throws Exception {
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
// 添加请求头
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
// 发送POST请求必须设置如下两行
con.setDoOutput(true);
con.setDoInput(true);
// 构建请求参数
StringBuilder sb = new StringBuilder();
for (String key : params.keySet()) {
sb.append(key).append("=").append(params.get(key)).append("&");
}
String urlParameters = sb.toString();
urlParameters = urlParameters.substring(0, urlParameters.length() - 1);
// 发送请求参数
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// 打印响应结果
System.out.println(response.toString());
return response.toString();
}
}
```
你可以使用该类中的方法进行HTTP和HTTPS请求。注意,对于HTTPS请求,需要使用`HttpsURLConnection`类。