java 发送http请求_java HttpClient 发送Http请求

所需maven jar包

org.apache.httpcomponents

httpclient

4.4.1

org.apache.httpcomponents

httpcore

4.4.1

发出get请求,可调用外部rest接口:

@org.junit.Test

public void testGet() throws IOException, Exception {

//创建HttpClient客户端

CloseableHttpClient httpClient = HttpClients.createDefault();

//创建请求方式 post get http://localhost:8888/demo/test/

String uri = "http://localhost:8888/demo/test/hello/cc/a";

HttpGet httpGet = new HttpGet(uri);

CloseableHttpResponse response = httpClient.execute(httpGet);

//相应结果

int statusCode = response.getStatusLine().getStatusCode();

System.out.println(statusCode);

HttpEntity entity = response.getEntity();

String string = EntityUtils.toString(entity);

System.out.println(string);

response.close();

httpClient.close();

}

发出post请求,模拟表单发请求:

@org.junit.Test

public void testPost() throws IOException, Exception {

//创建HttpClient客户端

CloseableHttpClient httpClient = HttpClients.createDefault();

//创建请求方式 post get

String uri = "http://localhost:8888/demo/test/testPost";

HttpPost httpPost = new HttpPost(uri);

//创建一个Entity,模拟一个表单

List list = new ArrayList();

list.add(new BasicNameValuePair("id", "1001"));

list.add(new BasicNameValuePair("name", "小黑"));

//把表单包装成一个HttpEntity对象

HttpEntity stringEntity = new UrlEncodedFormEntity(list,"utf-8");

//设置请求的内容

httpPost.setEntity(stringEntity);

CloseableHttpResponse response = httpClient.execute(httpPost);

//相应结果

int statusCode = response.getStatusLine().getStatusCode();

System.out.println(statusCode);

HttpEntity entity = response.getEntity();

String string = EntityUtils.toString(entity);

System.out.println(string);

response.close();

httpClient.close();

}

get请求添加其他参数,可参照:

//创建一个uri对象

URIBuilder uriBuilder = new URIBuilder("http://www.sogou.com/web");

uriBuilder.addParameter("query","花千骨");

HttpGet get = new HttpGet(uriBuilder.build());

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会为您介绍Java中使用CloseableHttpClient发送HTTP请求的方法。 CloseableHttpClient是Apache HttpClient库中的一个类,它提供了一种发送HTTP请求的简单方法。下面是一些基本的使用方法: 1. 创建CloseableHttpClient对象 ```java CloseableHttpClient httpClient = HttpClients.createDefault(); ``` 2. 创建HttpRequest对象 HttpRequest是一个接口,它定义了HTTP请求的各种方法和属性。在使用CloseableHttpClient发送HTTP请求之前,我们需要先创建一个HttpRequest对象。常用的HttpRequest实现类有HttpGet、HttpPost、HttpPut、HttpDelete等,具体选择哪种实现类取决于你要发送HTTP请求的类型。 例如,我们可以创建一个HttpGet对象: ```java HttpGet httpGet = new HttpGet("http://www.example.com"); ``` 3. 执行HTTP请求 创建好HttpRequest对象之后,我们就可以使用CloseableHttpClient对象来执行HTTP请求了。执行HTTP请求的方式很简单,只需要调用CloseableHttpClient对象的execute方法,并传入HttpRequest对象即可。 例如,我们可以执行上面创建的HttpGet对象: ```java CloseableHttpResponse response = httpClient.execute(httpGet); ``` 4. 处理HTTP响应 执行HTTP请求之后,我们需要处理HTTP响应。CloseableHttpResponse是一个接口,它定义了HTTP响应的各种方法和属性。我们可以使用这些方法来获取HTTP响应的状态码、响应头、响应体等信息。 例如,我们可以获取HTTP响应的状态码: ```java int statusCode = response.getStatusLine().getStatusCode(); ``` 5. 关闭CloseableHttpClient对象 最后,我们需要关闭CloseableHttpClient对象。关闭CloseableHttpClient对象可以释放与之相关的资源,例如HTTP连接、线程等。我们可以使用CloseableHttpClient对象的close方法来关闭它。 例如: ```java httpClient.close(); ``` 以上就是使用CloseableHttpClient发送HTTP请求的基本方法。希望能对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值