使用 Java 的 Net 库时如何伪造特定的浏览器客户端?

发布于 2024-07-27 02:04:18 字数 137 浏览 0 评论 0原文

我的一个小程序刚刚崩溃,因为我以编程方式浏览的网站现在假设 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

画尸师 2024-08-03 02:04:18

假设您使用的是 java.net.URLConnection,然后调用 setRequestProperty(String,String) 将请求标头设置为 IE 将使用的值。 例如,伪造IE6:

URL url = new URL("http://google.com");
URLConnection connection = url.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; .NET CLR 1.2.30703)");

然后像以前一样使用连接对象。

但 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:

URL url = new URL("http://google.com");
URLConnection connection = url.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; .NET CLR 1.2.30703)");

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

心作怪 2024-08-03 02:04:18

IIRC,通过命令行、JNLP 文件或其他地方的 System-D 设置 "http.agent" 系统属性。

IIRC, set "http.agent" system property through System, -D on the command line, in your JNLP file or elsewhere.

清眉祭 2024-08-03 02:04:18

您需要在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文