-1

I have the following method for reading a web page:

public String readURL() {

    String response="";
    try {
        URL url=new URL(urlString);
        BufferedReader br=new BufferedReader(new InputStreamReader(url.openStream()));
        response=br.readLine();
        br.close();
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null,"Unable to connect to server: "+e.toString(),windowTitle,JOptionPane.ERROR_MESSAGE);
        System.exit(-1);
    }
    return(response);
}

When I call this method the first time, it always works. However, when I call it a second time (within the same run) the InputStreamReader times out. The urlString is the same both times. I can rerun the application and the first call succeeds. I can use that urlString as many times in a row as I want from a browser and it always succeeds. It seems like there is something that I am not closing or some kind of clean up I need to do. What is the method missing in order to do subsequent calls? TIA.

Wt Riker
  • 514
  • 12
  • 24

1 Answers1

0

For me it worked fine, I just did

System.out.println(readURL());
System.out.println(readURL());

and that worked fine for me. The only thing I can think of is that you may need to close the InputStreamReader. The InputStream doesn't need to be closed, the javadoc said that InputStream.close() doesn't do anything.

EDIT: maybe the website doesn't like to be called twice in quick succession by a script (I don't know how a site would detect that but maybe it is possible). Try testing the script with a different site.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Pieter Mantel
  • 123
  • 1
  • 9
  • No need to close the InputStreamReader. "your code worked for me" answers aren't all that good, since it suggests that the problem lies elsewhere, and that the question in its present state is not answerable. – Hovercraft Full Of Eels Jul 11 '18 at 16:12
  • I'm not an expert at this sort of thing, so maybe it wasn't necessary. With "your code worked for me" indeed means that the problem lies somewhere else, but that doesn't make the answer bad. It encourages the one who asked the question to find the answer elsewhere (like my edit). – Pieter Mantel Jul 11 '18 at 17:05
  • Hmm. There is a difference that might suggest where the problem lies. The first call is done in 'main' and with a certain response coming back results in a JDialog being displayed. After the user fills in the form and clicks a submit button the button listener is the 2nd call to this method. Is there a problem doing network processing in a listener? – Wt Riker Jul 11 '18 at 18:29
  • @WtRiker: your problem isn't reproducible for us and you need to isolate it in the smallest code that does reproduce the problem, a [mcve], and then post this in your question. Otherwise all anyone can do is guess (as we're seeing here). – Hovercraft Full Of Eels Jul 11 '18 at 21:35
  • I would have to put my host in the url and I don't want to publish it in a public forum. Could I post the SSCCE and provide the url via PM? – Wt Riker Jul 11 '18 at 21:50
  • Never mind. I think I fixed the problem. I added code to retry the connection after a timeout. Probably slow server/network issue. – Wt Riker Jul 11 '18 at 22:33