The following snippet tries to send 2 parameters along with a URL to a servlet. But when I try this snippet,I get a message :
Connection to file server failed
But if I directly try the URL :
http://localhost:8084/nappster/ReceiveFileName?fileName=" + fileName + "&ip=" + IP
with the data, there is no problem. Servlet receives the file name and processes it as expected. What could be the reason that when I try to connect to the URL via code, it fails and succeeds when trying in a browser.
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String fileName = request.getParameter("FileTag");
String IP = new ClientAddress().getNetworkIP();
// Send the file name to the nappster server
URL url = new URL("http://localhost:8084/nappster/ReceiveFileName?fileName=" + fileName + "&ip=" + IP);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if(connection.getResponseCode() == 200) {
// File name sent successfully to the server
System.out.println("Connection to file server successful");
System.out.println("--------");
} else {
// Unable to send file name to the server
System.out.println("Connection to file server failed");
}
}
NOTE :
Response code returned when I try the above snippet is 505