java怎么发起HttpRequest请求,返回状态码和内容并解析json

github地址:https://github.com/furtech/java_utils/blob/master/src/main/java/com/furtech/javautils/HttpRequestUtil.java
代码中有详细说明(就不过多解释了)

依赖如下:

	//codeStatus
	compile 'commons-httpclient:commons-httpclient:3.1'
	// entity and codeStatus (recommended)
	implementation('org.apache.httpcomponents:httpclient:4.5.8')
	//json
	compile 'com.alibaba:fastjson:1.2.6'
package com.furtech.javautils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @des 发送 http request
 * you can encapsulate httpClient and set request body in json
 *
 * @author 719383495@qq.com | 719383495qq@gmail.com | 有问题可以邮箱或者github联系我
 * @date 2019/8/3 14:30
 */
public class HttpRequestUtil {

    public static void main(String[] args) {

        String url = "https://restapi.amap.com/v3/geocode/regeo?output=json&location=121.713683,31.379010&key=44d019ff91fd44b95e6bd251c7774673&radius=1000&extensions=all";
        String json = "";
        httpRequest0(url, json);
        httpRequest1(url, json);

    }

    /**
     * recommend using this
     * codeStatus
     * codeEntity
     *
     * @param url
     * @param json
     */
    private static void httpRequest1(String url, String json) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost post = new HttpPost(url);
        try {
            CloseableHttpResponse response = httpClient.execute(post);
            HttpEntity entity = response.getEntity();
            int statusCode = response.getStatusLine().getStatusCode();
            String content = EntityUtils.toString(entity);
            System.out.println(content + statusCode);
            JSONObject jsonObject = JSON.parseObject(content);
            System.out.println(jsonObject);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 第一种方式,只返回codeStatus
     *  200 true
     *  others false
     *
     * @param url
     * @param json
     */
    public static void httpRequest0(String url, String json) {
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod(url);
        RequestEntity requestEntity = null;
        try {
            requestEntity = new StringRequestEntity(json, "application/json", "utf-8");
            postMethod.setRequestEntity(requestEntity);

            int i = httpClient.executeMethod(postMethod);
            System.out.println(i);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱学习的小伙子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值