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();
        }
    }



}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券