/*以流的形式获取请求参数*/

本文介绍了一种通过ServletInputStream从HTTP请求中读取原始字节流的方法,并展示了如何使用这种方式处理POST请求中的数据。

RequestDemo5.java文件

package com.neu;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*以流的形式获取请求参数*/
//一般情况下不使用,文件上传时有时用
public class RequestDemo5 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        ServletInputStream in = request.getInputStream();
        int len = -1;
        byte b[] = new byte[1024];
        while((len=in.read(b)) != -1){
            System.out.println(new String(b,0,len));
        }
        in.close();
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);
    }

}

index.jsp文件:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
  <head>
    <title>注册页面</title>
  </head>

 <body>
    <form action="/Request/servlet/RequestDemo4" method="post">
        用户名:<input type="text" name="username"/><br/>
        密码:<input type="password" name="password"/><br/>
        确认密码:<input type="password" name="password"/><br/>
        <input type="submit" value="注册"/>
    </form>
  </body>

</html>

运行:
http://localhost:8080/Request/
运行结果:
封装前:User [username=null, password=null, gender=null]
封装后:User [username=ttt, password=[444, 666], gender=null]

sm4HexKey := "xxx" sm4keyBytes := codec.DecodeHex(sm4HexKey)~ // 将十六进制密钥转为字节数组 // hijackHTTPRequest 会在过滤后的请求到达Yakit MITM前被调用,可以通过该函数提前将请求修改或丢弃 // isHttps 请求是否为https请求 // url 网站URL // req 请求 // forward(req) 提交修改后的请求,如果未被调用,则使用原始的请求 // drop() 丢弃请求 hijackHTTPRequest = func(isHttps, url, req, forward /*func(modifiedRequest []byte)*/, drop /*func()*/) { println("oringinReq:",req) // 2. 提取并解析请求体 body := poc.GetHTTPPacketBody(req) println("原始请求体:", string(body)) if body !=nil && body.StartsWith( "request=") { // 3. 提取密文数据(去除"request="前缀) encryptedHex := string(body)[len("request="):] // 4. Base64解码(网络传输常用编码) encryptedBytes, err := codec.DecodeHex(encryptedHex) if err != nil { println("Hex解码失败:", err.Error()) return } // 5. SM4 ECB解密 decrypted, err := codec.Sm4ECBDecryptWithPKCSPadding(sm4keyBytes, encryptedBytes, nil) if err != nil { println("SM4解密失败:", err.Error()) return } println("解密后数据:", string(decrypted)) // 6. 重构请求 modifiedReq := poc.ReplaceHTTPPacketBody(req, decrypted) // println("modifiedReq:",modifiedReq) forward(modifiedReq) // 提交修改后的请求 } else { forward(req) // 无需修改直接转发 } } beforeRequest = func(ishttps /*bool*/, oreq /*[]byte*/, req/*[]byte*/){ req_method = poc.GetHTTPRequestMethod(req /*type: []byte*/) url = poc.GetUrlFromHTTPRequest("https" /*type: string*/, req /*type: []byte*/) req_body = poc.GetHTTPPacketBody(req) //req_body返回的是byte类型,codec的sm4加密函数需要传入string,加密完再转为hex println("tt:",json.dumps(req_body)) if req_method == "POST" && url.StartsWith("Example Domain") && req_body != nil { // 加密 encryptedvalue = codec.Sm4ECBEncryptWithPKCSPadding(sm4keyBytes /*type: []byte*/, string(req_body), nil /*type: []byte*/)~ encryptedHex = codec.EncodeToHex(encryptedvalue) encryptedBytes = "request=" + string(encryptedHex) req = poc.ReplaceBody(req /*type: []byte*/, []byte(encryptedBytes) /*type: []byte*/, false /*type: bool*/) return []byte(req) } } yakit热加载再增加了beforeRequest方法后,服务端返回500,不使用beforeRequest返回200,哪里出错了
最新发布
09-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值