Connection timed out only for wikmedia api on server but works on local

107 views Asked by At

Following piece of code was working for the last three years, but all of a sudden it throws connection timed out only in server, but works as intended in localhost. Any comments ?

    public  String getWikiContent(String query) {
    StringBuilder builder = new StringBuilder();

    String path = "https://en.wikipedia.org/w/api.php?action=query&prop=extracts&exintro=1&explaintext=1&titles=" + query + "&format=json&redirects";
    try {
        URL url = new URL(path);

        HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
        urlConn.setRequestProperty("Content-Type",
                "application/json");

        if (urlConn.getResponseCode() != 200) {
            throw new IOException(urlConn.getResponseMessage());
        }

        InputStream is = urlConn.getInputStream();
        BufferedReader buff = new BufferedReader(new InputStreamReader(is));
        String line;

        while ((line = buff.readLine()) != null) {
            builder.append(line);
        }
    }catch (IOException e){
        e.printStackTrace();
    }
    return builder.toString();
   }
1

There are 1 answers

0
Venkatesh Muthukrishnan On BEST ANSWER

For some reasons its a network issue, just adding a proxy server fixed it.

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(url,port));
URLConnection urlConn = url.openConnection(proxy);