最原始URLConnection方式如下,除此之外还有更加优化版的http请求方式,如HttpClient、okhttp3请求方式。
HttpClient案例详解:https://blog.csdn.net/justry_deng/article/details/81042379
okhttp3案例详解:https://blog.csdn.net/WoAiShuiGeGe/article/details/108325882
提供get、post两种方式
package com.example.consumer.controller.Http;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.Map;
public class RequestHttpUtil {
/**
* 向指定URL发送GET方法的请求
* 有参数或者无参数
* @param url 发送请求的URL
* @return 所代表远程资源的响应结果
*/
public static String sendGet(String url, Map requestHeader) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url;
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
if (requestHeader == null) {
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
//可设置header内的如token、密钥等功能,当然可以将此作为接口正式参数进行传递
conn.addRequestProperty("appkey","123465789");
} else {
// 设置自定义的请求属性
// connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
for (String key : requestHeader.keySet())
conn.setRequestProperty(key, requestHeader.get(key));
}
// 建立实际的连接
conn.connect();
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));// 防止乱码
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
/**
* 向指定URL发送POST方法的请求
*
* @param url 请求URL
* @param param 请求体字符串
* @return 响应结果
*/
public static String sendPost(String url, String param, Map requestheader) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = null;
conn = realUrl.openConnection();
conn.setConnectTimeout(2000);
conn.setReadTimeout(1000);
if (requestheader != null) {
// 自定义请求属性
for (String key : requestheader.keySet())
conn.setRequestProperty(key, requestheader.get(key));
} else {
// 设置通用的请求属性
conn.setRequestProperty("Content-Type","application/json");
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36");
//可设置header内的如token、密钥等功能,当然可以将此作为接口正式参数进行传递
conn.addRequestProperty("appkey","123465789");
}
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
e.printStackTrace();
}
// 使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
}
测试调用代码
/**
* get 无参
*/
public static void main(String[] args) throws Exception {
String result = RequestHttpUtil.sendGet("http://localhost:8001/getHttp", null);
System.out.println(result);
}
/**
* get 有参
*/
public static void main(String[] args) throws Exception {
String param = URLEncoder.encode("大家好");
String result = RequestHttpUtil.sendGet("http://localhost:8001/getHttpParam?param="+param, null);
System.out.println(result);
}
/**
* post 无参
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String result = RequestHttpUtil.sendPost("http://localhost:8001/postHttp", "",null);
System.out.println(result);
}
/**
* post 有参, json对象传值
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
JSONObject object = new JSONObject();
object.put("param","一个参数");
String result = RequestHttpUtil.sendPost("http://localhost:8001/postHttpParam", object.toJSONString(),null);
System.out.println(result);
}
接口地址代码
@Api(tags = "接口Http")
@RestController
public class HttpController {
@ApiOperation(value = "1.getHttp")
@GetMapping("/getHttp")
public String getHttp(){
return "get无参调用成功";
}
@ApiOperation(value = "2.getHttpParam")
@GetMapping("/getHttpParam")
public String getHttpParam(String param){
return "get有参调用成功,参数为:"+param;
}
@ApiOperation(value = "3.postHttp")
@PostMapping("/postHttp")
public String postHttp(){
return "post无参调用成功";
}
@ApiOperation(value = "4.postHttpJson")
@PostMapping("/postHttpJson")
public String getHttpParam(@RequestBody String param, HttpServletRequest request){
//获取header内的如密钥、token等自定义信息
String key = request.getHeader("appkey");
System.out.println(key);
return "post有参调用成功,参数为:"+param;
}
}