无法通过java应用程序访问https openstreetmap url,但可以通过浏览器和intellij“测试连接”访问

xt0899hw  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(307)

我正试着去https://nominatim.openstreetmap.org/ 通过我的java应用程序进行一些地址检索(目前正在intellij内部调试)。
基本上,在我的浏览器(即:chrome)和 Postman 上,我可以访问指向的地址,而在应用程序执行期间,我无法访问它。我还能够通过检查自动检测代理设置或通过设置有效的代理地址(问题发生时我在vpn后面),通过intellij test connection inside file>settings>system settings>http proxy成功测试指向url的连接。
我检查了一个代理(通过wpad url查找地址)并设置了 https.proxyHost 以及 https.proxyPort jvm选项(也可以通过 System.setProperty 在代码中),但几秒钟后仍然出现以下错误:

org.apache.http.conn.HttpHostConnectException: Connect to nominatim.openstreetmap.org:443 [nominatim.openstreetmap.org/130.117.76.9] failed: Connection timed out: connect
    at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:140) ~[httpclient-4.3.4.jar:4.3.4]
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:318) ~[httpclient-4.3.4.jar:4.3.4]
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363) ~[httpclient-4.3.4.jar:4.3.4]
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219) ~[httpclient-4.3.4.jar:4.3.4]
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195) ~[httpclient-4.3.4.jar:4.3.4]
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86) ~[httpclient-4.3.4.jar:4.3.4]
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108) ~[httpclient-4.3.4.jar:4.3.4]
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) ~[httpclient-4.3.4.jar:4.3.4]
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72) ~[httpclient-4.3.4.jar:4.3.4]
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:214) ~[httpclient-4.3.4.jar:4.3.4]
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:160) ~[httpclient-4.3.4.jar:4.3.4]
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:136) ~[httpclient-4.3.4.jar:4.3.4]
    at linear.skywalker.subjectupload.handlers.HttpClientHandler.executeGet(HttpClientHandler.java:173) ~[classes/:?]
    at linear.skywalker.subjectupload.randomsubject.AddressGenerator.getProvinceDescriptor(AddressGenerator.java:55) ~[classes/:?]
    at linear.skywalker.subjectupload.randomsubject.GenerateRandomSubject.generateRandomAddress(GenerateRandomSubject.java:206) ~[classes/:?]
    at linear.skywalker.subjectupload.randomsubject.GenerateRandomSubject.generate(GenerateRandomSubject.java:87) ~[classes/:?]
    at linear.skywalker.subjectupload.handlers.SubjectHandler.generateRandomSubject(SubjectHandler.java:125) ~[classes/:?]
    at linear.skywalker.subjectupload.Orchestrator.mainProcedure(Orchestrator.java:138) [classes/:?]
    at linear.skywalker.subjectupload.Orchestrator.orchestrateExecution(Orchestrator.java:211) [classes/:?]
    at linear.skywalker.subjectupload.Starter.main(Starter.java:75) [classes/:?]
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method) ~[?:1.8.0_221]
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) ~[?:1.8.0_221]
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[?:1.8.0_221]
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[?:1.8.0_221]
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[?:1.8.0_221]
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[?:1.8.0_221]
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[?:1.8.0_221]
    at java.net.Socket.connect(Socket.java:589) ~[?:1.8.0_221]
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:239) ~[httpclient-4.3.4.jar:4.3.4]
    at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:123) ~[httpclient-4.3.4.jar:4.3.4]
    ... 19 more
[INFO ] 2020-12-10 17:07:03.708 [main] Orchestrator - Subject at row [1] creation finished
[INFO ] 2020-12-10 17:07:03.708 [main] Orchestrator - Finished process creation
Disconnected from the target VM, address: '127.0.0.1:57975', transport: 'socket'

Process finished with exit code 0

从异常中可以看出,我使用的是通过 HttpClientBuilder.create().build() . 断开与vpn的连接会使代码正常工作,因此我怀疑我的设置有问题,或者缺少一些配置。我得到错误的地方如下:

HttpGet httpGet = new HttpGet(NATIM_SEARCH_BASE + "?q=" + province.replace(" ", "%20") + "+Italia"
                + "&format=json");

        httpGet.setHeader("User-Agent", <timestamp>);
        httpGet.setHeader("Referer", "http://www.example.com/1");
        httpGet.setHeader("Host", "localhost");

        return new JSONArray(httpClientHandler.executeGet(httpGet));

其中executeget只调用带有预构建responsehandler的httpclient对象上的execute。
有人知道我是否需要对代码执行额外的调整或设置吗?
和:有任何教程或工作的例子,这种情况?
谨致问候,阿尔贝托

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题