当前位置: 首页 > 工具软件 > Jodd > 使用案例 >

java使用Jodd-http发送http请求

苏健柏
2023-12-01

之前写过一篇关于java代码中使用restTemplate发送http请求的文章,那种方式比较复杂一点,现在使用jodd工具包来实现,相对来说简单多

首先在代码中引入jodd-http的依赖

  <!-- https://mvnrepository.com/artifact/org.jodd/jodd-http -->
        <dependency>
            <groupId>org.jodd</groupId>
            <artifactId>jodd-http</artifactId>
            <version>3.6.2</version>
        </dependency>

我其他地方(和http无关)还使用了jodd-core,所以也引入了,完整的pom依赖如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.hf</groupId>
    <artifactId>moredev</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>moredev</name>
    <description>spring boot多环境配置</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <!--<scope>test</scope>-->
        </dependency>

        <dependency>
            <groupId>org.jodd</groupId>
            <artifactId>jodd-core</artifactId>
            <version>3.9.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.jodd/jodd-http -->
        <dependency>
            <groupId>org.jodd</groupId>
            <artifactId>jodd-http</artifactId>
            <version>3.6.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.8.1</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

编写工具类,实现get/post请求的发送

package com.hf.moredev.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.Map;

/**
 * @Description: http发送工具类,使用jodd来实现
 * @Date: 2019/2/23
 * @Auther: 
 */
@Slf4j
public class HttpUtil {
    private static final ObjectMapper mapper = new ObjectMapper();
    /**
     *  发送get请求
     * @param url
     * @param params 无参数传递null
     * @return
     */
    public  static String sendHttpGet(String url, Map<String,String> params) throws Exception {
        HttpRequest request = HttpRequest.get(url);
        if(!CollectionUtils.isEmpty(params)){
            request.query(params);
        }
        HttpResponse response = request.send();
        String bodyText = response.bodyText();
        bodyText = transFormToUtf8(bodyText);
        return bodyText;
    }

    /**
     *
     * @param url
     * @param params 发送json格式的参数  无参传递null
     * @return
     */
    public static String sendHttpPostJosn(String url, Map<String,Object> params){
        HttpRequest request = HttpRequest.post(url);
        request.contentType("application/json");
        request.charset("utf-8");
        if(!CollectionUtils.isEmpty(params)){
            try {
                request.body(mapper.writeValueAsString(params));
            } catch (JsonProcessingException e) {
                log.error("write paramter [" + params.toString() + "] to josn fail",e.getMessage(),e);
            }
        }
        HttpResponse response = request.send();
        return response.bodyText();
    }

    /**
     *
     * @param url
     * @param params 发送文本参数 无参传递null
     * @return
     */
    public static String sendHttpPost(String url, Map<String,Object> params){
        HttpRequest request = HttpRequest.post(url);
        if(!CollectionUtils.isEmpty(params)) {
            request.form(params);
        }
        HttpResponse response = request.send();
        return response.bodyText();
    }


    /**
     * 字符串转换为utf-8输出
     * @param source
     * @return
     */
    private static String transFormToUtf8(String source) throws Exception{
        return new String(source.getBytes(),"utf-8");
    }
}

测试类:

package com.hf.moredev.mytest;

import com.hf.moredev.utils.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

/**
 * @Description:
 * @Date: 2019/2/23
 * @Auther:
 */
@Slf4j
public class AnyTest {

     @Test
    public void converterTest(){
         String url = "http://www.weather.com.cn/data/cityinfo/101010100.html";
         String result = null;
         try {
             result = HttpUtil.sendHttpGet(url, null);
         } catch (Exception e) {
            log.error("查询失败....." + url,e.getMessage(),e);
         }
         System.out.println(result);
     }
}

执行就可以查询成功了!

 

文末转载一篇关于:  jodd-core中操作日期的一些api,很实用(简化了很多的日期操作,很便利)

https://blog.csdn.net/mbh12333/article/details/78329490

 

 

 类似资料: