如何用Java解析HTTP请求?
在实现代理服务器时,我收到一个字符串形式的 HTTP 请求,如下所示:
获取 http://localhost:54321/x HTTP/1.1
主机:本地主机:54321
缓存控制:无缓存
是否有一个内置类来解析这个请求?
When implementing a proxy server, I get an HTTP request as a string, such as this one:
GET http://localhost:54321/x HTTP/1.1
Host: localhost:54321
Cache-Control: no-cache
Is there a built-in class to parse this request?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果“内置”意味着 JDK,我很确定没有任何类可以满足您的要求。
正如 Lukas 所说,HttpComponents 可能会有所帮助。即使没有类能够完全满足您的要求,也可以使用代码来了解它们是如何做到的。
不过,自己实现这一点并不难。
一些字符串分割就可以解决问题。
if "built in" means the JDK I am pretty sure there is no class that does you bidding.
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.
It is not very hard to implement this yourself though.
Some string splitting will do the trick.
我对这种解析的内置支持一无所知。如果你确实需要这样的解析器,你可以查看这个库: http://hc.apache.org/index .html
这是详细示例: http:// /www.mail-archive.com/[电子邮件受保护]/msg04070。 html
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
Here's detailed example: http://www.mail-archive.com/[email protected]/msg04070.html