I am try to making an java application which will be connected with a server and then try to access a link of that server page. For example, I have a link "http://goodserver.com" and I am able to connect with this url by this code
InetAddress addr = null;
Socket sock = new Socket("http://goodserver.com", 80);
addr = sock.getInetAddress();
System.out.println("Connected to " + addr);
Now I am also able to read the whole source code of this page. But there are button with links. When I go through a browser I can easily click on those button and go to that link. For example a button named "Test" and the corresponding link is "http://goodserver.com/targets/Test".
I want to access this link by java but the problem is that it can't be connected directly. I don't want to clcik this link by java as I have read this link "Programmatically click a webpage button" . I just want to know the mechanism by which a browser can access the link after loading the home page but its not possible through java http request.
I have read the page by this code
URL url = new URL("http://goodserver.com");
BufferedReader reader = new BufferedReader
(new InputStreamReader(url.openStream()));
BufferedWriter writer = new BufferedWriter
(new FileWriter("data.html"));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
writer.write(line);
writer.newLine();
}
reader.close();
writer.close();
When replace this home page link with my target button link "http://goodserver.com/targets/Test" I am getting the home page source code not the target page.
I know that a browser also send http requests to get pages then it should be possible by java. Thanks in advance.