分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

HttpClient之基本使用

发布时间:2023-09-06 02:33责任编辑:顾先生关键词:暂无标签

增加依赖配置

<dependency> ???<groupId>org.apache.httpcomponents</groupId> ???<artifactId>httpclient</artifactId> ???<version>4.5</version></dependency><dependency> ???<groupId>org.apache.httpcomponents</groupId> ???<artifactId>httpcore</artifactId> ???<version>4.4.5</version></dependency>
package com.zns.test;import java.net.URI;import java.util.ArrayList;import java.util.List;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.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;import org.apache.log4j.Logger;public class Test { ???protected static Logger logger = Logger.getLogger(Test.class); ???public static void main(String[] args) throws Exception { ???????test1(); ???} ???// get请求 ???public static void test1() throws Exception { ???????// 创建Httpclient对象 ???????CloseableHttpClient httpclient = HttpClients.createDefault(); ???????// 创建http GET请求 ???????// HttpGet httpGet = new HttpGet("http://www.baidu.com/s?wd=java"); ???????// 含有参数 ???????URI uri = new URIBuilder("http://www.baidu.com/s").setParameter("wd", "java").build(); ???????HttpGet httpGet = new HttpGet(uri); ???????CloseableHttpResponse response = null; ???????try { ???????????// 执行请求 ???????????response = httpclient.execute(httpGet); ???????????// 判断返回状态是否为200 ???????????if (response.getStatusLine().getStatusCode() == 200) { ???????????????// 获取服务端返回的数据 ???????????????String content = EntityUtils.toString(response.getEntity(), "UTF-8"); ???????????????System.out.println(content); ???????????} ???????} finally { ???????????if (response != null) { ???????????????response.close(); ???????????} ???????????httpclient.close(); ???????} ???} ???// post请求 ???public static void test2() throws Exception { ???????// 创建Httpclient对象 ???????CloseableHttpClient httpclient = HttpClients.createDefault(); ???????// 创建http POST请求 ???????HttpPost httpPost = new HttpPost("http://www.xxx.com/search"); ???????// 设置请求参数 ???????List<NameValuePair> parameters = new ArrayList<NameValuePair>(); ???????parameters.add(new BasicNameValuePair("id", "1")); ???????parameters.add(new BasicNameValuePair("name", "abc")); ???????// 构造一个form表单式的实体 ???????UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters); ???????// 将请求实体设置到httpPost对象中 ???????httpPost.setEntity(formEntity); ???????// 如果是JSON数据 ???????// String jsonBody=""; ???????// StringEntity jsonEntity = new StringEntity(jsonBody, "utf-8"); ???????// jsonEntity.setContentType("application/json"); ???????// httpPost.setEntity(jsonEntity); ???????// 伪装浏览器请求 ???????httpPost.setHeader("User-Agent", ???????????????"Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36"); ???????CloseableHttpResponse response = null; ???????try { ???????????// 执行请求 ???????????response = httpclient.execute(httpPost); ???????????// 判断返回状态是否为200 ???????????if (response.getStatusLine().getStatusCode() == 200) { ???????????????// 获取服务端返回的数据 ???????????????String content = EntityUtils.toString(response.getEntity(), "UTF-8"); ???????????????System.out.println(content); ???????????} ???????} finally { ???????????if (response != null) { ???????????????response.close(); ???????????} ???????????httpclient.close(); ???????} ???}}

HttpClient之基本使用

原文地址:https://www.cnblogs.com/zengnansheng/p/10389812.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved