java表单类爬虫

server/2024/10/21 9:47:36/

见代码附件

编写方式1

 public static void Posts() throws IOException{// 创建 HTTP POST 请求HttpPost httpPost = new HttpPost("http://www.cninfo.com.cn/new/disclosure");// 设置请求头httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");// 构建要发送的数据字符串,动态替换 pageNum 参数String pageNum = "1"; // 假设要发送的 pageNum 值为 3String postData = String.format("column=szse_gem_latest&pageNum=%s&pageSize=30&sortName=&sortType=&clusterFlag=true", pageNum);// 设置实体为要发送的数据字符串httpPost.setEntity(new StringEntity(postData));// 创建 HttpClient 并发送请求try (CloseableHttpClient httpClient = HttpClients.createDefault()) {HttpResponse response = httpClient.execute(httpPost);// 处理响应int statusCode = response.getStatusLine().getStatusCode();if (statusCode == 200) {// 读取响应内容String responseBody = EntityUtils.toString(response.getEntity());System.out.println("响应内容:" + responseBody);} else {System.err.println("请求失败,状态码:" + statusCode);}}}

编写方式2

用列表设置参数,采用UrlEncodedFormEntity 进行编码

public static void PostForm() throws IOException {// 2. 设置表单参数List<NameValuePair> kv = new ArrayList<>();kv.add(new BasicNameValuePair("column", "szse_gem_latest"));kv.add(new BasicNameValuePair("pageNum", "1"));kv.add(new BasicNameValuePair("pageSize", "30"));kv.add(new BasicNameValuePair("sortName", ""));kv.add(new BasicNameValuePair("sortType", ""));kv.add(new BasicNameValuePair("clusterFlag", "true"));// 3. 创建HttpPost实例HttpPost httpPost = new HttpPost("http://www.cninfo.com.cn/new/disclosure");httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36");// 4. 让Post携带表单参数httpPost.setEntity(new UrlEncodedFormEntity(kv, Consts.UTF_8));// 5. 获取HttpResponse响应CloseableHttpResponse response = httpClient.execute(httpPost);// 6. 读responseSystem.out.println(EntityUtils.toString(response.getEntity()));// 7. 释放资源response.close();httpClient.close();}

http://www.ppmy.cn/server/4992.html

相关文章

HarmonyOS开发实例:【分布式新闻客户端】

介绍 本篇Codelab基于栅格布局、设备管理和多端协同&#xff0c;实现一次开发&#xff0c;多端部署的分布式新闻客户端页面。主要包含以下功能&#xff1a; 展示新闻列表以及左右滑动切换新闻Tab。点击新闻展示新闻详情页。点击新闻详情页底部的分享按钮&#xff0c;发现周边…

Games104 现代游戏引擎3

Sprite Animation 序列帧动画 自由度&#xff08;degrees of freedom&#xff0c;DoF&#xff09;对于刚体而言描述它的运动需要3个位移3个旋转&#xff0c;一共6个自由度 顶点动画&#xff08;per-vertex animation&#xff09;利用网格的顶点来控制运动。此时网格上的每个顶…

客户端动态降级系统

本文字数&#xff1a;4576字 预计阅读时间&#xff1a;20分钟 01 背景 无论是iOS还是Android系统的设备&#xff0c;在线上运行时受硬件、网络环境、代码质量等多方面因素影响&#xff0c;可能会导致性能问题&#xff0c;这一类问题有些在开发阶段是发现不了的。如何在线上始终…

达梦数据库导入导出工具dmfldr

达梦数据库导入导出工具dmfldr 基础信息 OS版本&#xff1a; Red Hat Enterprise Linux Server release 7.9 (Maipo) DB版本&#xff1a; DM Database Server 64 V8 DB Version: 0x7000c 03134284132-20240115-215128-200811 dmfldr工具介绍 dmfldr&#xff08;DM Fast Loade…

设计模式代码实战-观察者模式

1、问题描述 小明所在的学校有一个时钟&#xff08;主题&#xff09;&#xff0c;每到整点时&#xff0c;它就会通知所有的学生&#xff08;观察者&#xff09;当前的时间&#xff0c;请你使用观察者模式实现这个时钟通知系统。 注意点&#xff1a;时间从 0 开始&#xff0c;并…

Jackson 2.x 系列【25】Spring Boot 集成之起步依赖、自动配置

有道无术&#xff0c;术尚可求&#xff0c;有术无道&#xff0c;止于术。 本系列Jackson 版本 2.17.0 本系列Spring Boot 版本 3.2.4 源码地址&#xff1a;https://gitee.com/pearl-organization/study-jaskson-demo 文章目录 1. 前言2. 起步依赖3. 自动配置3.1 JacksonPrope…

Uni-app中实现数据选择并回传给上个页面的方法

当我们在Uni-app中进行页面间数据传递时&#xff0c;通常会涉及到数据的选择以及回传给上个页面的需求。为了达到这个目的&#xff0c;我们可以利用Uni-app提供的事件机制和页面导航方法来实现。以下是一种实现方式&#xff1a; 数据选择并回传给上个页面的方法 第一步&#…

Go语言中通过数据对齐降低内存消耗和提升性能

数据对齐是一种安排数据分配方式以加速 CPU 访问内存的方法。 不了解这个概念会导致额外的内存消耗甚至性能下降。 要了解数据对齐的工作原理,让我们首先讨论没有它会发生什么。假设我们分配两个变量,一个 int32 类型的 (32 B) 和一个 int64类型的(64 B): var i int32…