如何在 Java 中解析 HTTP 请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5625388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to parse an HTTP Request in Java?
提问by Amir Rachum
When implementing a proxy server, I get an HTTP request as a string, such as this one:
在实现代理服务器时,我收到一个字符串形式的 HTTP 请求,例如这个:
GET http://localhost:54321/xHTTP/1.1
Host: localhost:54321
Cache-Control: no-cache
获取http://localhost:54321/xHTTP/1.1
主机:本地主机:54321
缓存控制:无缓存
Is there a built-in class to parse this request?
是否有内置类来解析这个请求?
采纳答案by Lukasz
I don't know anything about built-in support for such parsing. If you really need such parser, you can check out this lib: http://hc.apache.org/index.html
我对这种解析的内置支持一无所知。如果你真的需要这样的解析器,你可以查看这个库:http: //hc.apache.org/index.html
Here's detailed example: http://www.mail-archive.com/[email protected]/msg04070.html
这是详细示例:http: //www.mail-archive.com/[email protected]/msg04070.html
回答by Mikael Vandmo
if "built in" means the JDK I am pretty sure there is no class that does you bidding.
如果“内置”是指 JDK,我很确定没有您要竞标的课程。
As Lukas says, HttpComponents might be of help. Even if there is no class that does exactly what you want there is code available to look at how they do it.
正如 Lukas 所说,HttpComponents 可能会有所帮助。即使没有完全按照您的要求执行操作的类,也可以使用代码来查看它们是如何做到的。
It is not very hard to implement this yourself though. Some string splitting will do the trick.
不过,自己实现这一点并不难。一些字符串拆分可以解决问题。