使用 Java 的 Net 库时如何伪造特定的浏览器客户端?
我的一个小程序刚刚崩溃,因为我以编程方式浏览的网站现在假设 Java 请求来自移动电话,而我要查找的链接不在他们的移动页面上。
所以我想伪造一个 Internet Explorer 访问权限。 我如何使用 java.net 做到这一点?
A small program of mine just broke because, it seems, the site I was programmatically browsing now assumes a Java request comes from a mobile phone, and the link I was looking for is not on their mobile page.
So I want to fake an Internet Explorer access. How do I do that with java.net?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您使用的是 java.net.URLConnection,然后调用 setRequestProperty(String,String) 将请求标头设置为 IE 将使用的值。 例如,伪造IE6:
然后像以前一样使用连接对象。
但 java.net 太可怕了。 使用 Apache Commons HttpClient 代替,它要好得多。
更好的是,使用专为导航网站而设计的框架,例如 HtmlUnit
Assuming you're using java.net.URLConnection, then call setRequestProperty(String,String) to set the request header to a value that IE would use. For example, to fake IE6:
and then use the connection object as before.
But java.net is horrible. Use Apache Commons HttpClient instead, it's much nicer.
Even better, use a framework designed for navigating websites, like HtmlUnit
IIRC,通过命令行、JNLP 文件或其他地方的
System
、-D
设置"http.agent"
系统属性。IIRC, set
"http.agent"
system property throughSystem
,-D
on the command line, in your JNLP file or elsewhere.您需要在 HTTP 中设置 User-Agent 标头请求 Internet Explorer 使用的值。
我建议使用 Jakarta HttpClient 库 发出请求,因为它提供了更高级别的请求操纵请求。
You need to set the User-Agent header in the HTTP request to a value used by Internet Explorer.
I recommend using the Jakarta HttpClient library to make the request as it provides a higher level API for manipulating the request.