如何在 Java 中使用 Http、Socks4 和 Socks5 代理?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2077931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:18:22  来源:igfitidea点击:

How to use Http, Socks4 and Socks5 proxies in java?

javaproxyhttprequest

提问by Yatendra Goel

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:

我想对网站进行屏幕抓取,为此我想使用 Http、Socks4 和 Sock5 代理。所以我的问题如下:

  1. 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 HttpURLConnectionthrough theseproxies?

  2. If it is not possible, then What other external APIs I can use?

  3. I was doing it by using a headless browser provided by HtmlUnitbut 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.

  1. 是否可以通过 Java 使用这些代理而不使用任何其他外部 API?例如,是否可以通过这些代理通过HttpURLConnection发送请求?

  2. 如果不可能,那么我可以使用哪些其他外部 API?

  3. 我是通过使用HtmlUnit提供的无头浏览器来做到这一点的,但是加载即使是简单的网页也需要时间,所以请您向我推荐其他提供快速加载网页的无头浏览器的 API(如果有)。我不想打开包含大量 AJAX 或 Javascript 代码的网页。我只需要通过无头浏览器单击表单按钮。

采纳答案by Pascal Thivent

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 HttpURLConnectionthrough these proxies?

是否可以通过 Java 使用这些代理而不使用任何其他外部 API?例如,是否可以HttpURLConnection通过这些代理发送请求?

Yes, you can configure proxies by either using (global) system properties, or using the Proxyclass, or using a ProxySelector. The two later options are available since Java 5 and are more flexible. Have a look at Java Networking and Proxiesas mentioned by jarnbjo for all the details.

是的,您可以通过使用(全局)系统属性、使用Proxy类或使用ProxySelector. 后面的两个选项自 Java 5 起可用,并且更加灵活。看看Java的网络和代理由jarnbjo的所有细节提及。

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.

我是通过使用 HtmlUnit 提供的无头浏览器来做到这一点的,但是加载即使是简单的网页也需要时间,所以请您向我推荐其他提供快速加载网页的无头浏览器的 API(如果有)。我不想打开包含大量 AJAX 或 Javascript 代码的网页。我只需要通过无头浏览器单击表单按钮。

Unfortunately, the first alternatives I can think of are either HtmlUnit based (like JWebUnitor WebTest) or slower (Selenium, WebDriver- that you can run in headless mode). But maybe you could try HttpUnitif you don't need advanced JavaScript support.

不幸的是,我能想到的第一个替代方案是基于 HtmlUnit(如JWebUnitWebTest)或速度较慢(SeleniumWebDriver- 您可以在无头模式下运行)。但如果您不需要高级 JavaScript 支持,也许您可以尝试HttpUnit

回答by jarnbjo

Yes, that is possible. You can find the configuration options for different network proxies here.

是的,这是可能的。您可以在此处找到不同网络代理的配置选项。