Java发送Http请求之——发送Get请求

1.POM依赖

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.5.2</version>
        </dependency>

        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>

2.发送Get请求工具类

package com.example.demo.util;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.List;

public class HttpUtil {

    public static String sendGetReq(String url, List<NameValuePair> nameValuePairList, String encoding, String cookie, String authorization) throws Exception {
        String body = "";

        //创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        /**
         * 创建URIBuilder
         */
        URIBuilder uriBuilder = new URIBuilder(url);

        if (nameValuePairList != null){
            /**
             * 设置参数
             */
            uriBuilder.addParameters(nameValuePairList);
        }

        HttpGet httpGet = new HttpGet(uriBuilder.build());
        //设置header信息
        httpGet.setHeader ("Accept", "application/json, text/javascript, */*; q=0.01");
        httpGet.setHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");

        if (StringUtils.isNotBlank(cookie)){
            httpGet.setHeader("Cookie", cookie);
        }

        if (StringUtils.isNotBlank(authorization)){
            httpGet.setHeader("Authorization", authorization);
        }

        //执行请求操作,并拿到结果(同步阻塞)
        CloseableHttpResponse response = httpClient.execute(httpGet);
        //获取结果实体
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            //按指定编码转换结果实体为String类型
            body = EntityUtils.toString(entity, encoding);
        }
        //释放链接
        response.close();
        return body;
    }

    public static void main(String[] args) {
        String url = "http://localhost:8080/receiveGetReq";

        List<NameValuePair> reqParams = new ArrayList<>();
        reqParams.add(new BasicNameValuePair("userName","发送get请求"));
        reqParams.add(new BasicNameValuePair("password","123456"));

        try {
            String result1 = sendGetReq (url, reqParams, "utf-8",null,null);
            System.out.println (result1);

        } catch (Exception e) {
            e.printStackTrace ( );
        }
    }

}

3.验证是否请求成功

package com.example.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

    @GetMapping("/receiveGetReq")
    public String receiveGetReq(
            @RequestParam(value = "userName") String userName,
            @RequestParam(value = "password") String password
    ){
        System.out.println("userName="+userName+";password="+password);
        return "请求成功";
    }

}

4.总结

请求成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值