Java HttpURLConnection在Windows上有效,在Linux上失败 [英] Java HttpURLConnection works on Windows and fails on Linux

查看:160
本文介绍了Java HttpURLConnection在Windows上有效,在Linux上失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java Spark应用程序,它可以从网站检索数据,如下所示:

I have a Java Spark application that retrieves data from a Website as follows:

while(true)
        {
            try{    
                connection = (HttpURLConnection) uRL.openConnection();
                /* optional default is GET */
                connection.setRequestMethod("GET");

                /* add request header */
                connection.setRequestProperty("User-Agent", USER_AGENT);
                connection.getResponseCode();
                connection.setReadTimeout(0);
                /* Read the response code */
                bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
                break;
            }
            catch(Exception e){
                LOGGER.error("Error in querying Wikipedia: "+e.getMessage());
                continue;
            }
        }
        response = new StringBuffer();
        while ((inputLine = bufferedReader.readLine()) != null) {
            response.append(inputLine);
            response.append("\n");
        }
        bufferedReader.close();

此代码在Windows上运行良好.

This code works well on Windows.

但是,在具有HTTP和HTTPs代理服务器的Centos计算机上,它因连接超时而失败.我为应用程序的HTTPs代理设置了系统属性,并确保它适用于某些链接.但是,它不适用于其他某些人.对于那些行不通的应用程序,我还在Linux服务器上使用wget尝试了相同的URL并成功运行.
无效的链接:

However, on a Centos machine which has an HTTP and HTTPs proxy server, it fails with Connection Timeout. I set the system Properties for the HTTPs Proxy for the application and make sure it works for some links. However, it doesn't work for some others. For those it doesn't work, I also tried the same URL using wget on the linux server and worked.
Link that doesn't work: https://ar.wikipedia.org/w/api.php?action=query&format=xml&titles=%D9%82%D8%B1%D9%89&redirects&prop=pageprops|categories&cllimit=500
link that works: https://ar.wikipedia.org/w/api.php?action=query&format=xml&list=allpages&apnamespace=14&apfilterredir=nonredirects&aplimit=500

推荐答案

Java不一定遵守系统的默认代理设置.由于您可以在Linux计算机上卷曲" URL,因此最可能的解释是Java没有使用您配置的代理.以下链接介绍了配置Java代理的各种方法:

Java doesn't necessarily respect your system's default proxy settings. Since you are able to "curl" the URL on the Linux machine, the most likely explanation is that Java is not using the proxy that you have configured. The following links explains various ways to configure the proxies for Java:

  • How do I set the proxy to be used by the JVM - describes approaches using system properties.
  • How do I configure proxy settings for Java? (Oracle) - describes how to do it with the Java control panel.

这篇关于Java HttpURLConnection在Windows上有效,在Linux上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆