简体   繁体   English

如何用Java解析HTTP请求?

[英]How to parse an HTTP Request in Java?

When implementing a proxy server, I get an HTTP request as a string, such as this one: 实现代理服务器时,我得到一个HTTP请求作为字符串,例如:

GET http://localhost:54321/x HTTP/1.1 GET http:// localhost:54321 / x HTTP / 1.1

Host: localhost:54321 主持人:localhost:54321

Cache-Control: no-cache 缓存控制:无缓存

Is there a built-in class to parse this request? 是否有内置类来解析此请求?

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 如果你真的需要这样的解析器,你可以查看这个lib: http//hc.apache.org/index.html

Here's detailed example: http://www.mail-archive.com/httpclient-user@jakarta.apache.org/msg04070.html 以下是详细示例: http//www.mail-archive.com/httpclient-user@jakarta.apache.org/msg04070.html

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. 正如Lukas所说,HttpComponents可能会有所帮助。 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. 一些字符串拆分可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM