首页 > 编程语言 >JAVA-Exploit编写(2)--HttpClient库使用

JAVA-Exploit编写(2)--HttpClient库使用

时间:2025-01-18 19:28:07浏览次数:3  
标签:HttpGet http -- import Exploit new apache JAVA response

目录

1. HttpClient简介

1.1 Apache HttpClient 特性

1.2 Apache HttpClient 使用流程

2. 依赖导入

3. HttpClient属性

4. GET方式请求

4.1 test2.php

4.2 不携带参数请求 

4.3 携带参数提交

4.4 支持URIBuilder对象的使用 

5. POST请求

5.1 test3.php

5.2 单个参数提交

5.3 多个参数提交 

5.4 设置请求的配置信息 

6. 设置代理访问 

6.1 test4.php

6.2 在配置中添加代理

7. 处理异常  


1. HttpClient简介

        Httpclient是客户端的http通信实现库,这个类库的作用是接收和发送http报文,使用这个类库,它相比传统的HttpURLConnection增加了灵活性和易用性,对与http的操作会简单一些

        Httpclient 支持了在 HTTP /1.1 规范中定义的所有 HTTP 方法:GET,HEAD.POST, PUT, DELETETRACE和 OPTIONS。对于每个方法类型,都有一个特定的类来支持:HttpGet, HttpHead,HttpPost,HttpPut, HttpDelete和 HttpOptions.

1.1 Apache HttpClient 特性

        基于标准、纯净的 Java 语言。实现了 HTTP 1.0 和 HTTP 1.1

        以可扩展的面向对象的结构实现了 HTTP 全部的方法(GET, POST,PUT,DELETE,HEAD, OPTIONS,andTRACE)。

支持 HTTPS 协议。

通过 HTTP 代理建立透明的连接。

        利用 CONNECT 方法通过 HTTP 代理建立隧道的 HITPS 连接。Basic, Digest, NTLMV1, NTLMV2, NTLM2 Session, SNPNEGO/Kerberos 认证方案,中便携可靠的套接字工厂使它更容易的使用第三方解决力案。连接管埋器支持多线程应用。支持设置最大连接数,同时支持设置每个主机的最大连接数,发现并关闭过期的连接.

插件式的白定义认证方案。

自动处理 Set-cookie 中的 Cookie。

插件式的自定义 Cookie 策略。

Request 的输出流可以避免流中内容直接缓冲到 Socket 服务器.

Response 的输入流可以有效的从 Socket 服务器直接读取相应内容。

在 HTTP 1.0 和 HTTP 1.1 中利用 KeepAlive 保持持久连接。

直接获取服务器发送的 response code 和 headers。

设置连接超时的能力。

实验性的支持 HTTP 1.1 response caching。

源代码基于 Apache License 可免费获取。

1.2 Apache HttpClient 使用流程

使用 Httpclient 发送请求、接收响应很简单,一般需要如下几步即:

创建HttpClient 对象

创建请求方法的实例,并指定请求 URL.如果需要发送 GET 请求,创建 HttpGet 对象;如果需要发送POST 请求,创建 HttpPost 对象。

如果需要发送请求参数,可调用 HttpGet、HttpPost 共同的 setParams(HttpParams params)方法来添加请求参数;对于 HttpPost 对象而言,也可调用,setEntity()

调用HttpClient 对象excute()发送请求,该请求会返回一个HttpResponse对象

调用 HttpResponse 的 getAllHeaders0)、getHeaders(String name)等方法可获取服务器的响应头;调用HttpResponse 的 getEntity() 方法可获取 HttpEntity 对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。

释放连接。无论执行方法是否成功,都必须释放连接

2. 依赖导入

在创建项目时,通过Maven进行构建.

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

3. HttpClient属性

//创建什么方法就new什么对象
new HttpGet() 
new HttpPost()
  
//创建一个HttpGet对象
HttpGet httpGet = new HttpGet(urlstr);

//设置请求的参数(需要什么设置什么)
httpGet.setHeader("Content-Type","application/json");
httpGet.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0); Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3");

//发送请求
        CloseableHttpResponse response = httpClient.execute(httpGet);
        if(response.getStatusLine().getStatusCode() == 200){
            // 获得响应的正文
            response.getEntity();
            //获取响应实体
            res = EntityUtils.toString(response.getEntity());
        }
        return res;
    }

4. GET方式请求

4.1 test2.php

<?php
var_dump($_GET);

4.2 不携带参数请求 

package com.deger;

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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class HttpclientTest {
    public static void main(String[] args) throws IOException {
        System.out.println(doget("http://127.0.0.1/test2.php?username=hacker"));
    }
    public static String doget(String urlstr) throws IOException {
        //接收值
        String res = null;

        //HttpClients对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建一个HttpGet对象
        HttpGet httpGet = new HttpGet(urlstr);
        new HttpPost();
        //设置请求头
        httpGet.setHeader("Content-Type","application/json");
        httpGet.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0); Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3");

        //发送请求
        CloseableHttpResponse response = httpClient.execute(httpGet);
        if(response.getStatusLine().getStatusCode() == 200){
            // 获得响应的正文
            response.getEntity();
            //获取响应实体
            res = EntityUtils.toString(response.getEntity());
        }
        return res;
    }
}

4.3 携带参数提交

//增加了在参数列表中的值,以及在

package com.deger;

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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class HttpclientTest {
    public static void main(String[] args) throws IOException {
//        System.out.println(doget("http://127.0.0.1/test2.php?username=hacker"));
        System.out.println(doget("http://127.0.0.1/test2.php","hacker"));
    }
    public static String doget(String urlstr,String params) throws IOException {
        //接收值
        String res = null;

        //HttpClients对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建一个HttpGet对象
//        HttpGet httpGet = new HttpGet(urlstr);
        //携带参数提交
        HttpGet httpGet = new HttpGet(urlstr + "?"+ params);
        new HttpPost();
        //设置请求头
        httpGet.setHeader("Content-Type","application/json");
        httpGet.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0); Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3");

        //发送请求
        CloseableHttpResponse response = httpClient.execute(httpGet);
        if(response.getStatusLine().getStatusCode() == 200){
            // 获得响应的正文
            response.getEntity();
            //获取响应实体
            res = EntityUtils.toString(response.getEntity());
        }
        return res;
    }
}

4.4 支持URIBuilder对象的使用 

URIBuilder uri = new URIBuilder();
 //URIBuilder对象
        URIBuilder uri = new URIBuilder(urlstr);
        uri.addParameter("age","20");
        //携带参数提交
//        HttpGet httpGet = new HttpGet(urlstr + "?" + params);
        HttpGet httpGet = new HttpGet(uri.build());

5. POST请求

5.1 test3.php

<?php
var_dump($_POST);

5.2 单个参数提交

public static String dopost(String urlstr) throws IOException, URISyntaxException {
        //接收值
        String res = null;

        //HttpClients对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建一个HttpGet对象
//        HttpGet httpGet = new HttpGet(urlstr);

        //URIBuilder对象
        URIBuilder uri = new URIBuilder(urlstr);
        //携带参数提交
        HttpPost httpPost = new HttpPost(uri.build());

        //设置请求头
        httpPost.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0); Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3");
        httpPost.setHeader("Content-Type","application/json");
        httpPost.setHeader("Content-Type","application/x-www-form-urlencoded");

        ArrayList<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("name","hacker"));
        //转为form表单的编码数据
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params,"UTF-8");
        //设置实体正文
        httpPost.setEntity(entity);

        //发送请求
        CloseableHttpResponse response = httpClient.execute(httpPost);
        if(response.getStatusLine().getStatusCode() == 200){
            // 获得响应的正文
            response.getEntity();
            //获取响应实体
            res = EntityUtils.toString(response.getEntity());
        }
        return res;
    }

5.3 多个参数提交 

public static void main(String[] args) throws Exception {
//        System.out.println(doget("http://127.0.0.1/test2.php?username=hacker"));
//        System.out.println(doget("http://127.0.0.1/test2.php","hacker"));
        //批量传递参数值
        Map<String, String> map = new HashMap<>();
        map.put("username","hacker");
        map.put("age","20");

        System.out.println(dopost("http://127.0.0.1/test3.php",map));
    }
public static String dopost(String urlstr, Map<String,String> queryMap) throws IOException, URISyntaxException {
        //接收值
        String res = null;

        //HttpClients对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        //URIBuilder对象
        URIBuilder uri = new URIBuilder(urlstr);
        //携带参数提交
        HttpPost httpPost = new HttpPost(uri.build());

        //设置请求头
        httpPost.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0); Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3");
        httpPost.setHeader("Content-Type","application/json");
        httpPost.setHeader("Content-Type","application/x-www-form-urlencoded");

        ArrayList<NameValuePair> params = new ArrayList<>();

        for (Map.Entry<String, String> query : queryMap.entrySet()) {
            params.add(new BasicNameValuePair(query.getKey(), query.getValue()));
        }
        //转为form表单的编码数据
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params,"UTF-8");
        //设置实体正文
        httpPost.setEntity(entity);

        //发送请求
        CloseableHttpResponse response = httpClient.execute(httpPost);
        if(response.getStatusLine().getStatusCode() == 200){
            // 获得响应的正文
            response.getEntity();
            //获取响应实体
            res = EntityUtils.toString(response.getEntity());
        }
        return res;
    }

5.4 设置请求的配置信息 

  //URIBuilder对象
        URIBuilder uri = new URIBuilder(urlstr);
        //设置请求的链接信息
        RequestConfig config = RequestConfig.custom()
                //请求的连接超时时间
                .setConnectTimeout(5000)
                //设置socket的超时时间
                .setSocketTimeout(5000)
                //获取链接的最长时间  连接池
                .setConnectionRequestTimeout(5000)
                .build();
        //携带参数提交
        HttpPost httpPost = new HttpPost(uri.build());
        //设置配置信息
        httpPost.setConfig(config);

6. 设置代理访问 

6.1 test4.php

<?php
var_dump($_GET);
var_dump($_POST);

6.2 在配置中添加代理

 //设置代理访问
        HttpHost proxy = new HttpHost("127.0.0.1", 8888);
        RequestConfig config = RequestConfig.custom()
                //请求的连接超时时间
                .setConnectTimeout(5000)
                //设置socket的超时时间
                .setSocketTimeout(5000)
                //获取链接的最长时间  连接池
                .setConnectionRequestTimeout(5000)
                //设置代理
                .setProxy(proxy)
                .build();

        httpGet.setConfig(config);

还是在burp中增加一个代理,然后抓包 


7. 处理异常  

将使用频繁的对象在开始时进行创建并赋值,在后续使用中较为方便,通过try-catch处理异常.

CloseableHttpClient httpClient = null;

CloseableHttpResponse response = null;

package com.deger;

import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
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 java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class HttpclientTryCatch {
    public static void main(String[] args) throws Exception {
//        System.out.println(doget("http://127.0.0.1/test2.php?username=hacker"));
        System.out.println(doget("http://127.0.0.1/test0.php","name=hacker"));
        //批量传递参数值
        Map<String, String> map = new HashMap<>();
        map.put("username","hacker");
        map.put("age","20");

        System.out.println(dopost("http://127.0.0.1/test0.php",map));
    }
    public static String doget(String urlstr,String params) {
        //接收值
        String res = null;
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;

        //HttpClients对象
        httpClient = HttpClients.createDefault();
        //创建一个HttpGet对象
        HttpGet httpGet = new HttpGet(urlstr +"?"+ params);


        //设置代理访问
        HttpHost proxy = new HttpHost("127.0.0.1", 8888);
        RequestConfig config = RequestConfig.custom()
                //请求的连接超时时间
                .setConnectTimeout(5000)
                //设置socket的超时时间
                .setSocketTimeout(5000)
                //获取链接的最长时间  连接池
                .setConnectionRequestTimeout(5000)
                //设置代理
                .setProxy(proxy)
                .build();

        httpGet.setConfig(config);
        //设置请求头
        httpGet.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0); Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3");
        httpGet.setHeader("Content-Type","application/json");

        //发送请求
        try {
            response = httpClient.execute(httpGet);
            if(response.getStatusLine().getStatusCode() == 200){
                // 获得响应的正文
                response.getEntity();
                //获取响应实体
                res = EntityUtils.toString(response.getEntity());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                //关闭资源
                response.close();
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        return res;
    }

    public static String dopost(String urlstr, Map<String,String> queryMap) throws IOException, URISyntaxException {
        //接收值
        String res = null;

        //HttpClients对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        //URIBuilder对象
        URIBuilder uri = new URIBuilder(urlstr);
        //设置请求的链接信息
        RequestConfig config = RequestConfig.custom()
                //请求的连接超时时间
                .setConnectTimeout(5000)
                //设置socket的超时时间
                .setSocketTimeout(5000)
                //获取链接的最长时间  连接池
                .setConnectionRequestTimeout(5000)
                .build();
        //携带参数提交
        HttpPost httpPost = new HttpPost(uri.build());
        //请求的配置信息
        httpPost.setConfig(config);

        //设置请求头
        httpPost.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0); Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3");
        httpPost.setHeader("Content-Type","application/json");
        httpPost.setHeader("Content-Type","application/x-www-form-urlencoded");

        ArrayList<NameValuePair> params = new ArrayList<>();

        for (Map.Entry<String, String> query : queryMap.entrySet()) {
            params.add(new BasicNameValuePair(query.getKey(), query.getValue()));
        }
        //转为form表单的编码数据
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params,"UTF-8");
        //设置实体正文
        httpPost.setEntity(entity);

        //发送请求
        CloseableHttpResponse response = httpClient.execute(httpPost);
        if(response.getStatusLine().getStatusCode() == 200){
            // 获得响应的正文
            response.getEntity();
            //获取响应实体
            res = EntityUtils.toString(response.getEntity());
        }
        return res;
    }
}

 

标签:HttpGet,http,--,import,Exploit,new,apache,JAVA,response
From: https://blog.csdn.net/weixin_72543266/article/details/145176021

相关文章

  • Vue3 自定义Hooks完全指南
    目录1.前言2.什么是Hooks2.1Hooks的定义2.2为什么需要Hooks2.3与Vue2的区别3.Hooks的实现原理3.1响应式系统3.2生命周期集成3.3依赖注入系统4.Hooks的作用与应用场景4.1常见应用场景4.2实际案例分析5.Hooks的优缺点5.1优点5.2缺点6.Hooks的书写规范6......
  • “洋悟运动”之己见
    作为常驻B站人,之前我对小红书的印象是什么?AI图片集散地?国内版Instagram?还是“生活记录”?就在两天前,外国人就占据了这款软件的半壁江山。我很疑惑。实际上,我一直对这次的“暴发”有一个猜想。是否存在这些内容全部为AI生成的可能?至少,在一开始得知这次事件时,我是这么......
  • if-else对比switch-case
    概述在编程中,控制流语句用于控制程序的执行路径。if-else和 switch-case是两种常见的控制流语句,分别适用于不同的场景。了解它们的区别和最佳使用场景,有助于编写更高效、可读性更强的代码。if-else结构1.基本语法if-else语句根据布尔表达式的结果来选择执行不同的代码块。其......
  • 寒假学习日记8
    今天主要是有关服务器查看自己系统和版本 .使用uname命令uname命令可以提供关于系统的基本信息。查看操作系统名称:uname-o查看操作系统的版本和内核版本:uname-a要查看服务器的架构(即处理器架构),你可以使用以下几种方法:.使用uname-m命令uname-m会显......
  • 有一个包含开始时间和结束时间的数组,要求日期从早到晚有连贯性,不能出现重叠,用JAVA判断
    packagecom.cfb.oa.m;importjava.time.LocalDate;importjava.util.ArrayList;importjava.util.List;classDateRange{LocalDatestart;LocalDateend;publicDateRange(LocalDatestart,LocalDateend){this.start=start;th......
  • 使用 AWS CLI 管理 EMR
    AmazonEMR(ElasticMapReduce)是一种托管的大数据处理服务,使用户能够在云上便捷、快速地运行和管理大规模数据分析和处理任务。创建EMR集群创建默认IAM角色:awsemrcreate-default-roles查询EMR版本:awsemrlist-release-labels创建EMR集群:awsemrcrea......
  • 将IDEA的setter代码模板改成链式setter
    setter传统模式UserInfouserInfo=newUserInfo();userInfo.setUserId("zhangsan");userInfo.setUserName("张三");userInfo.setAge(18);每一行都需要分号来隔断,影响编码效率。链式setterUserInfouserInfo=newUserInfo().setUserId("zhangsan").setUserNam......
  • 图书馆管理系统javaweb(含数据库脚本)
    图书馆管理系统javaweb(含数据库脚本),tomcat7eclipsejdk1.8包含数据库文件列表BookLibrarySystem-master/.classpath , 9020BookLibrarySystem-master/.project , 1639BookLibrarySystem-master/.settings/.jsdtscope , 499BookLibrarySystem-master/.settings/org.ec......
  • 长期更新IDEA安装永久破解教程
    IntelliJIDEA版本亲测Version2024.3.2✔Version2024.3.1✔Version2024.2.*✔介绍JetBrains是一家专注于创建智能开发工具的前沿软件公司,旗下常用的软件有IntelliJIDEA、PhpStorm、PyCharm、Rider、RubyMine、RustRover、WebStorm、Goland、CLion等。IntelliJID......
  • PKUWC2025 游记
    PKUWC2025游记Day-30从whk的苦海中脱离出来。Day-5校内考了一次模拟赛,发挥不尽人意,如此状态,如何进队?Day-2,-1周1早上的飞机,在家爽玩1天半。Day0轻装上阵,以为20号就回学校了,只带了一套衣服。在飞机上玩元气。真服了江之永矣……我们的酒店在绍兴,他给我们拐......