httpclient个人理解

摘要:
Httpclient:模拟浏览器发送请求,服务器将响应数据。CAR区域网络中不同系统之间的请求调用取决于httpclient。jar和httpcore。jar,它需要在<dependency><groupId>org中是纯的。阿帕奇。httpcomponentshttpclientpackagecom.taot

httpclient:模拟浏览器发送请求,服务器会响应数据,用心区域网内 不同系统间的请求调用

依赖  httpclient.jar和httpcore.jar需要同时纯在

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

httpclient个人理解第1张

package com.taotao.httpclient;

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

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
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 org.junit.Test;

public class HttpClientTest {

    @Test
    public void doGet() throws Exception {
        //创建一个httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建一个GET对象
        HttpGet get = new HttpGet("http://www.sogou.com");
        //执行请求
        CloseableHttpResponse response = httpClient.execute(get);
        //取响应的结果
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println(statusCode);
        HttpEntity entity = response.getEntity();
        String string = EntityUtils.toString(entity, "utf-8");
        System.out.println(string);
        //关闭httpclient
        response.close();
        httpClient.close();
    }
    
    @Test
    public void doGetWithParam() throws Exception{
        //创建一个httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建一个uri对象
        URIBuilder uriBuilder = new URIBuilder("http://www.sogou.com/web");
        uriBuilder.addParameter("query", "花千骨");
        HttpGet get = new HttpGet(uriBuilder.build());
        //执行请求
        CloseableHttpResponse response = httpClient.execute(get);
        //取响应的结果
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println(statusCode);
        HttpEntity entity = response.getEntity();
        String string = EntityUtils.toString(entity, "utf-8");
        System.out.println(string);
        //关闭httpclient
        response.close();
        httpClient.close();
    }
    
    @Test
    public void doPost() throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();

        //创建一个post对象
        HttpPost post = new HttpPost("http://localhost:8082/httpclient/post.action");
        //执行post请求
        CloseableHttpResponse response = httpClient.execute(post);
        String string = EntityUtils.toString(response.getEntity());
        System.out.println(string);
        response.close();
        httpClient.close();
        
    }
    
    @Test
    public void doPostWithParam() throws Exception{
        CloseableHttpClient httpClient = HttpClients.createDefault();
     
        //创建一个post对象  
        HttpPost post = new HttpPost("http://localhost:8082/httpclient/post.action");
        //创建一个Entity。模拟一个表单
        List<NameValuePair> kvList = new ArrayList<>();
        kvList.add(new BasicNameValuePair("username", "张三"));
        kvList.add(new BasicNameValuePair("password", "123"));
        
        //包装成一个Entity对象 
        StringEntity entity = new UrlEncodedFormEntity(kvList, "utf-8");
        //设置请求的内容 
        post.setEntity(entity);
        
        //执行post请求
        CloseableHttpResponse response = httpClient.execute(post);
        String string = EntityUtils.toString(response.getEntity());
        System.out.println(string);
        response.close();
        httpClient.close();
    }
}

免责声明:文章转载自《httpclient个人理解》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇汽车网络处理设计微信小程序样式旋转下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

防止表单重复提交

1.方法一 在servlet中模拟网络延迟造成表单重复提交 //解决表单重复提交方法 //接收数据 String username = req.getParameter("username"); System.out.println("接收的数据为:"+username);...

C# String小技巧

NET string 类是相当广泛的,然而仍有一些String函数是缺失的或者不是很明显,在使用Net String时,这里提供几个小技巧. 用重复的字符填充一个 String 为了用重复的字符填充一个字符串, 使用string 类的构造函数.例如:用20个(*)填充一个字符串. strings=newstring('*',20); 检查空字符串 一...

logstash中关于Jdbc输入配置选项详解

Setting Input type Required clean_run boolean No columns_charset hash No connection_retry_attempts number No connection_retry_attempts_wait_time number No jdbc_connec...

AutoMapper(三)

返回总目录 自定义类型转换 有时,需要完全控制一个类型到另一个类型的转换。一个类型一点都不像另一个类型,而且转换函数已经存在了,在这种情况下,你想要从一个“宽松”的类型转换成一个更强壮的类型,例如一个string的源类型到一个int32的目标类型。 这里有两个类Source和Destination,要把前者映射到后者,代码如下: public class...

VB 的字符串处理函数

一、InStr 返回 Variant (Long),指定一字符串在另一字符串中最先出现的位置。 语法 InStr([start, ]string1, string2[, compare]) start 可选参数。为数值表达式,设置每次搜索的起点。如果省略,将从第一个字符的位置开始。如果 start 包含 Null,将发生错误。如果指定了 compar...

Odoo 接口

1. 很多人还是习惯使用restful格式进行接口操作,但odoo已默认jsonrpc,所以需要专门写一个装饰器 def json_response(func): """返回去除封装的JSON""" @wraps(func) def decorate(*args, **kwargs): request.__raw_j...