栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java中发送Http请求之HttpURLConnection

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java中发送Http请求之HttpURLConnection

Java中发送Http请求之HttpURLConnection
  • 1 HttpURLConnection
        • 1 准备一个SpringBoot项目环境
        • 2 添加一个控制器
        • 3 添加一个发送请求的工具类
        • 4 添加测试工具类
        • 5 测试结果

Java中发送Http请求的方式有很多,记录一下相关的请求方式,本次记录Jdk自带的HttpURLConnection,简单简洁,使用简单.

1 HttpURLConnection

HttpURLConnection是Jdk自带的请求工具,不用依赖第三方jar包,适用简单的场景使用.

使用方式是, 通过调用URL.openConnection方法,得到一个URLConnection对象,并强转为HttpURLConnection对象.

java.net.URL部分源码:

    public URLConnection openConnection() throws java.io.IOException {
        return handler.openConnection(this);
    }

java.net.HttpURLConnection部分源码:

abstract public class HttpURLConnection extends URLConnection {
    // ...
}

从代码可知HttpURLConnection是URLConnection子类, 其内类中主要放置的是一些父类的方法和请求码信息.

发送GET请求, 其主要的参数从URI中获取,还有请求头,cookies等数据.

发送POST请求, HttpURLConnection实例必须设置setDoOutput(true),其请求体数据写入由HttpURLConnection的getOutputStream()方法返回的输出流来传输数据.

1 准备一个SpringBoot项目环境 2 添加一个控制器
@Controller
@Slf4j
public class HelloWorld {

    @Override
    @GetMapping("/world/getData")
    @ResponseBody
    public String getData(@RequestParam Map param) {
        System.out.println(param.toString());
        return " Hello World  getData 方法";
    }

    @PostMapping("/world/getResult")
    @ResponseBody
    public String getResult(@RequestBody Map param) {
        System.out.println(param.toString());
        return " Hello World  getResult 方法";
    }
}
3 添加一个发送请求的工具类
package com.cf.demo.http;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;


@Slf4j
@Data
public class JdkHttpUtils {

    
    public static String getPost(String url, Integer connectTimeout,
            Integer readTimeout, String contentType, Map heads,
            Map params) throws IOException {

        URL u;
        HttpURLConnection connection = null;
        OutputStream out;
        try {
            u = new URL(url);
            connection = (HttpURLConnection) u.openConnection();
            connection.setRequestMethod("POST");
            connection.setConnectTimeout(connectTimeout);
            connection.setReadTimeout(readTimeout);
            connection.setRequestProperty("Content-Type", contentType);
            // POST请求必须设置该属性
            connection.setDoOutput(true);
            connection.setDoInput(true);
            if (heads != null) {
                for (Map.Entry stringStringEntry : heads.entrySet()) {
                    connection.setRequestProperty(stringStringEntry.getKey(),
                            stringStringEntry.getValue());
                }
            }

            out = connection.getOutputStream();
            if (params != null && !params.isEmpty()) {
                out.write(toJSONString(params).getBytes());
            }
            out.flush();
            out.close();

            // 获取请求返回的数据流
            InputStream is = connection.getInputStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            // 封装输入流is,并指定字符集
            int i;
            while ((i = is.read()) != -1) {
                baos.write(i);
            }
            return baos.toString();

        } catch (Exception e) {
            log.error("请求发生异常,信息为= {} ", e.getMessage());
        }
        return null;
    }


    
    public static String getGet(String url, Integer connectTimeout,
            Integer readTimeout, String contentType, Map heads,
            Map params) throws IOException {

        // 拼接请求参数
        if (params != null && !params.isEmpty()) {
            url += "?";
            if (params != null && !params.isEmpty()) {
                StringBuilder sb = new StringBuilder();
                for (Map.Entry stringObjectEntry : params.entrySet()) {
                    try {
                        sb.append(stringObjectEntry.getKey()).append("=").append(
                                URLEncoder.encode(stringObjectEntry.getValue(), "UTF-8"))
                                .append("&");
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
                sb.delete(sb.length() - 1, sb.length());
                url += sb.toString();
            }
        }

        URL u;
        HttpURLConnection connection;
        u = new URL(url);
        connection = (HttpURLConnection) u.openConnection();
        connection.setRequestMethod("GET");
        connection.setConnectTimeout(connectTimeout);
        connection.setReadTimeout(readTimeout);
        connection.setRequestProperty("Content-Type", contentType);
        if (heads != null) {
            for (Map.Entry stringStringEntry : heads.entrySet()) {
                connection.setRequestProperty(stringStringEntry.getKey(),
                        stringStringEntry.getValue());
            }
        }

        // 获取请求返回的数据流
        InputStream is = connection.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        // 封装输入流is,并指定字符集
        int i;
        while ((i = is.read()) != -1) {
            baos.write(i);
        }
        return baos.toString();

    }


    
    public static String toJSONString(Map map) {
        Iterator> i = map.entrySet().iterator();
        if (!i.hasNext()) {
            return "{}";
        }

        StringBuilder sb = new StringBuilder();
        sb.append('{');
        for (; ; ) {
            Map.Entry e = i.next();
            String key = e.getKey();
            String value = e.getValue();
            sb.append(""");
            sb.append(key);
            sb.append(""");
            sb.append(':');
            sb.append(""");
            sb.append(value);
            sb.append(""");
            if (!i.hasNext()) {
                return sb.append('}').toString();
            }
            sb.append(',').append(' ');
        }
    }


}
4 添加测试工具类
@Slf4j
public class HttpTest {

    // 请求地址
    public String url = "";
    // 请求头参数
    public Map heads = new HashMap<>();
    // 请求参数
    public Map params = new HashMap<>();
    // 数据类型
    public String contentType = "application/json";
    // 连接超时
    public Integer connectTimeout = 15000;
    // 读取超时
    public Integer readTimeout = 60000;

    @Test
    public void testGET() {
        // 1 添加数据
        url = "http://localhost:8080/world/getData";
        heads.put("token", "hhhhhhhhhhhaaaaaaaa");
        params.put("username", "libai");

        String result = null;
        try {
            // 2 发送Http请求,获取返回结果
            result = JdkHttpUtils
                    .getGet(url, connectTimeout, readTimeout, contentType, heads, params);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 3 打印结果
        log.info(result);
    }

    @Test
    public void testPOST() {
        // 1 添加数据
        url = "http://localhost:8080/world/getResult";
        heads.put("token", "hhhhhhhhhhhaaaaaaaa");
        params.put("username", "libai");

        String result = null;
        try {
            // 2 发送Http请求,获取返回结果
            result = JdkHttpUtils
                    .getPost(url, connectTimeout, readTimeout, contentType, heads, params);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 3 打印结果
        log.info(result);
    }
}    
5 测试结果

POST请求测试结果

 

GET请求测试结果

 

参考资料:

https://www.baeldung.com/java-http-request

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/571341.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号