在项目中,为了实现一些特定的功能,我们常常需要发送http异步请求
,为此需要特意封装一个实用的HttpUtils工具类
HttpUtils工具类内容如下:
package com.zyw.seckill.utils;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
/**
* @Author Mr.zhou
* @Date 2021/3/27
* @Description http请求发送工具类
*/
public class HttpUtils {
/**
* 向指定URL发送GET方法的请求
*
* @param url 发送请求的URL
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return URL 所代表远程资源的响应结果
*/
public static String doGetHttp(String url, String param) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.<