Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

221
Views
Java HttpURLConnection works on Windows and fails on Linux

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();

This code works well on Windows.

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

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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.
over 4 years ago · Santiago Trujillo Report

0

I'm using Ubuntu and It worked with me

    try {
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");

        //add request header
        int responseCode = con.getResponseCode();

        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        System.out.println(response.toString());
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!