如何在java中使用Http、Socks4和Socks5代理?
我想对网站进行屏幕抓取,为此我想使用 Http、Socks4 和 Sock5 代理。所以我的问题如下:
是否可以通过 Java 使用这些代理而不使用任何其他外部 API?例如,是否可以通过 发送请求HttpURLConnection 通过这些代理?
如果不可能,那么我还可以使用哪些其他外部 API?
我使用 HtmlUnit 提供的无头浏览器来完成此操作,但即使加载简单的网页也需要时间,那么您能否建议我提供其他可快速加载网页的无头浏览器 API(如果有)。我不想打开包含大量 AJAX 或 Javascript 代码的网页。我只需要通过无头浏览器单击表单按钮。
I want to screen-scrape a website and for that I want to use Http, Socks4 and Sock5 proxies. So my questions are as follows:
Is it possible to use these proxies through Java without using any other external API? For instance, Is it possible to send a request through HttpURLConnection through theseproxies?
If it is not possible, then What other external APIs I can use?
I was doing it by using a headless browser provided by HtmlUnit but it takes time to load even simple webpages, so could you please suggest me other APIs (if any) that provide headless browsers that are fast in loading webpages. I don't want to open webpages that contain heavy AJAX or Javascript code. I just need to click on the forms button through the headless browser.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,您可以通过使用(全局)系统属性或使用
Proxy
类,或使用ProxySelector
。后面的两个选项从 Java 5 开始就可用,并且更加灵活。查看 Java 网络和代理正如 jarnbjo 所提到的所有细节。不幸的是,我能想到的第一个替代方案是基于 HtmlUnit 的(例如 JWebUnit 或 WebTest)或更慢(Selenium、WebDriver - 您可以在无头模式下运行)。但如果您不需要高级 JavaScript 支持,也许您可以尝试 HttpUnit 。
Yes, you can configure proxies by either using (global) system properties, or using the
Proxy
class, or using aProxySelector
. The two later options are available since Java 5 and are more flexible. Have a look at Java Networking and Proxies as mentioned by jarnbjo for all the details.Unfortunately, the first alternatives I can think of are either HtmlUnit based (like JWebUnit or WebTest) or slower (Selenium, WebDriver - that you can run in headless mode). But maybe you could try HttpUnit if you don't need advanced JavaScript support.
是的,这是可能的。您可以在此处找到不同网络代理的配置选项< /a>.
Yes, that is possible. You can find the configuration options for different network proxies here.
您可以设置每个连接代理。以下是 Java 11
HttpClient
和旧版HttpURLConnection
的示例:您可以使用 SOCKS 或 HTTP 代理。
您可以在此处阅读有关 Java 代理的更多信息:https:// /docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html
You can set up per-connection proxies. Here is an example with the Java 11
HttpClient
and the legacyHttpURLConnection
:You can use either SOCKS or HTTP proxying.
You can read more on Java proxying here: https://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html