java使用Jodd-http发送http请求

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

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

  
        
            org.jodd
            jodd-http
            3.6.2
        

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



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.2.RELEASE
         
    
    com.hf
    moredev
    0.0.1-SNAPSHOT
    moredev
    spring boot多环境配置

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            
        

        
            org.jodd
            jodd-core
            3.9.1
        

        
        
            org.jodd
            jodd-http
            3.6.2
        

        
        
            org.apache.commons
            commons-lang3
            3.8.1
        


    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


编写工具类,实现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 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 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 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

 

 

你可能感兴趣的:(spring,boot)