java 使用 HttpClient post 提交表单

zq910515 2019-08-20 07:33:45
修改路由器网络信息,浏览器post 提交表单,使用抓包工具抓取的数据如下:






上图中的表单数据格式为什么找不到IP等信息?
想问问,java代码使用HttpClient 发送post请求修改路由器的网络,需要怎么写代码?
我想模拟浏览器提交表单的方式,提交java代码中的变量,达到修改路由器网络的目的。


我测试代码如下:

public void updateNet(String url) {
System.out.println();
// 封装post请求参数
StringBody token = new StringBody("c87bcca1f66d6d968e0c318cf89a22e4", ContentType.TEXT_PLAIN);
StringBody cbi_submit = new StringBody("1", ContentType.TEXT_PLAIN);
StringBody tab_network_wan6 = new StringBody("general", ContentType.TEXT_PLAIN);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("token", token);
reqEntity.addPart("cbi.submit", cbi_submit);
reqEntity.addPart("tab.network.wan6", tab_network_wan6);


// 创建httpClient实例
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建httpPost远程连接实例
HttpPost httpPost = new HttpPost(url);
// 配置请求参数实例
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 设置连接主机服务超时时间
.setConnectionRequestTimeout(35000)// 设置连接请求超时时间
.setSocketTimeout(60000)// 设置读取数据连接超时时间
.build();
// 为httpPost实例设置配置
httpPost.setConfig(requestConfig);
// 设置请求头
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");

// 为httpPost设置封装好的请求参数
httpPost.setEntity(reqEntity);


CloseableHttpResponse httpResponse = null;
try {

// httpClient对象执行post请求,并返回响应参数对象
httpResponse = httpClient.execute(httpPost,httpClientContext);
for(Header header:httpResponse.getAllHeaders()){
System.out.println(header.getName()+" : "+header.getValue());
}
System.out.println();
System.out.println("statusCode : "+httpResponse.getStatusLine().getStatusCode());

// 从响应对象中获取响应内容
HttpEntity entity = httpResponse.getEntity();


} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭资源
if (null != httpResponse) {
try {
httpResponse.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != httpClient) {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}



...全文
995 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
瘦死的黑骆驼 2019-08-20
  • 打赏
  • 举报
回复
怎么没有呢,不是有host?你请求的目标地址,还有referer,表示你第一次打开浏览输入的网址 想模拟浏览器提交,你就先在浏览器里正常访问一下,然后把消息头里的东西都带上就好了嘛,现在很多网站为了过滤无效访问,是需要对消息头里的部分信息进行验证的,比如Referer、User-Agent
rumlee 2019-08-20
  • 打赏
  • 举报
回复
这种还需要抓什么包啊,直接用浏览器审查功能看一下就好了。 一般步骤可能都是这样 1、提交管理账号和密码到路由器登录的地址,登录成功之后获取cookie 2、带上cookie提交数据修改网络 这其实就是一个非常简单的需要登录的爬虫程序。

67,541

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧