gpt4 book ai didi

java - 替换 HTTP 请求字符串中的 Host

转载 作者:行者123 更新时间:2023-12-01 04:54:29 25 4
gpt4 key购买 nike

给定一个 HTTP 请求:

Client: GET /Default.aspx HTTP/1.1
Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave
-flash, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-
xpsdocument, application/xaml+xml, */*
Accept-Language: en-us
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET
CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)
Accept-Encoding: gzip, deflate
Host: logan-pc:8080
Connection: Keep-Alive

如何以编程方式更改 Host 行?我尝试过使用正则表达式(使用 this 作为指导),但无法找出正确的模式。

这是我现在使用的模式:

".*?Host:.*?:" + <dynamic port number goes here>+ ".*?"

编译模式时,我按照链接问题中的建议使用 Pattern.DOTALL 标志。

更新:我直接从浏览器获取请求字符串,这就是我现在使用的:

int len = 0;
while((len = ConnHandler.this.clientIn.read(buf)) > 0)
{
System.out.println(".*?Host:.*?:" + ConnHandler.this.serverport+".*?");
String in = new String(buf, 0, len);
in.replaceAll(".*?Host:.*?:" + ConnHandler.this.serverport+".*?", "localhost:" + ConnHandler.this.serverport);
//...
}

但它仍然不起作用。

最佳答案

你的模式是正确的。这段代码对我有用:

String string = "Accept-Encoding: gzip, deflate\nHost: logan-pc:8080\nConnection: Keep-Alive";
System.out.println(string.replaceAll(".*?Host:.*?:8080.*?", "Host: NEW_HOST:8080"));

输出:

Accept-Encoding: gzip, deflate

Host: NEW_HOST:8080

Connection: Keep-Alive

关于java - 替换 HTTP 请求字符串中的 Host,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14399799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com