httpClient发送get和post请求

 <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.5.8</version>
    </dependency>

  

【get请求】

package com.xt.postman;

import org.apache.http.HttpEntity;
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 javax.swing.text.html.parser.Entity;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Author: TestRookie * @Date: 2021/7/18 15:26 * @Description: *
 */
public class TestGet {
    public static void main(String[] args) throws IOException {
        /**
         * 1.设置url
         * 2.设置请求方式,创建对应的请求方式对象
         * 3.创建httpClient对象
         * 4.发送请求
         * 5.获取报文对象
         * 6.报文对象格式化的输出
         */
        String url = "http://test.lemonban.com/ningmengban/app/login/login.html";
        HttpGet get = new HttpGet(url);
        CloseableHttpClient client = HttpClients.createDefault();
        CloseableHttpResponse response = client.execute(get);
        HttpEntity entity = response.getEntity();
        InputStream content = entity.getContent();//流对象 read
        /*byte[] bytes = new byte[1024];
        while(content.read(bytes)!=-1){
            System.out.println(new String(bytes));
        }*/
        String result = EntityUtils.toString(entity);
        System.out.println(result);
    }
}

  

【post请求】

package com.xt.postman;

import org.apache.http.HttpEntity;
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.util.EntityUtils;

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

/**
 * @Author: TestRookie * @Date: 2021/7/18 15:58 * @Description: *
 */
public class TestPost {
    public static void main(String[] args) throws IOException {
        /**
         * 1.设置url
         * 2.设置请求方式,创建对应的请求方式对象
         * 3.创建httpClient对象
         * 4.绑定参数
         * 5.发送请求
         * 6.获取报文对象
         * 7.格式化报文
         */
        String url="http://test.lemonban.com/ningmengban/mvc/user/login.json";
        HttpPost post = new HttpPost(url);
        //设置请求头 注意参数要以form表单的形式提交
        post.setHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
        CloseableHttpClient client = HttpClients.createDefault();
        //绑定参数
        post.setEntity(new StringEntity("username=13311112222&password=4297f44b13955235245b2497399d7a93"));
        CloseableHttpResponse response = client.execute(post);
        HttpEntity entity = response.getEntity();
        String result = EntityUtils.toString(entity);
        System.out.println(result);
    }
}

  

StringEntity stringEntity = new StringEntity(JSONObject.toJSONString(params), "UTF-8");

posted @ 2021-07-18 17:12  Mr_sven  阅读(168)  评论(0编辑  收藏  举报