HttpClient

参考文献:HttpClient基本使用-CSDN博客

GET

package com.tungkong.httpclient;


import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class HttpClientGet {
    //https://blog.csdn.net/yangsf_/article/details/124527687
    public static void main(String[] args) throws IOException {
        // 1. 创建HttpClient实例
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 2. 创建GET请求方法实例
        HttpGet httpGet = new HttpGet("http://172.16.196.187:8666/sns/oauth2/access_token");
        // 3. 调用HttpClient实例来执行GET请求方法,得到response
        CloseableHttpResponse response = httpClient.execute(httpGet);
        // 4. 读response,判断是否访问成功 2xx表示 成功。
        int status = response.getStatusLine().getStatusCode();
        if (status >= 200 && status < 300) {
            // 对得到后的实例可以进行处理,例如读取回复体,读取html
            HttpEntity entity = response.getEntity();
            System.out.println(response);
            System.out.println("=======================");
            String html = EntityUtils.toString(entity);
            System.out.println(html);
        } else {
            throw new ClientProtocolException("Unexpected response status: " + status);
        }

        // 6. 释放连接
        response.close();
        httpClient.close();
    }


}

POST

package com.tungkong.httpclient;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
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.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HttpClientPost {
    public static void main(String... args) throws IOException {
        // 1. 创建HttpClient实例
        CloseableHttpClient httpclient = HttpClients.createDefault();

        // 2. 设置表单参数
        List<NameValuePair> kv =  new ArrayList<>();
        kv.add(new BasicNameValuePair("name", "root"));
        kv.add(new BasicNameValuePair("password", "123456"));

        // 3. 创建HttpPost实例
        HttpPost httpPost = new HttpPost("http://172.16.196.187:8666/sns/oauth2/access_token");
        httpPost.setEntity(new StringEntity("this is Post"));

        // 4. 让Post携带表单参数
        httpPost.setEntity(new UrlEncodedFormEntity(kv, Consts.UTF_8));

        // 5. 调用HttpClient实例来执行HttpPost实例
        CloseableHttpResponse response = httpclient.execute(httpPost);

        // 6. 读 response
        int status = response.getStatusLine().getStatusCode();
        if (status >= 200 && status < 300) {
            HttpEntity entity = response.getEntity();
            System.out.println(response);
            System.out.println("===================");
            String html = EntityUtils.toString(entity);
            System.out.println("html="+html);
        } else {
            throw new ClientProtocolException("Unexpected response status: " + status);
        }
        // 5. 释放连接
        response.close();
        httpclient.close();
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值