Reputation: 37
A URL which is accessible from the browser gives a 404 respond code when run in java code.
What is the problem??? ..Can anyone explain me about this
public String login(String url) {
URL targetURL;
long start = 0;
long end = 0;
float difference = 0;
HttpURLConnection httpURLConnection;
StringBuffer strbufstatus = new StringBuffer();
try {
//Connecting to the url
targetURL = new URL(url);
start = System.currentTimeMillis();
httpURLConnection = (HttpURLConnection) targetURL.openConnection();
httpURLConnection.setUseCaches(false);
httpURLConnection.setAllowUserInteraction(false);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("GET");
httpURLConnection.connect();
//Getting the respond Code
int responseCode = httpURLConnection.getResponseCode();
strbufstatus.append("Response Code===> " + responseCode + "<br>");
if(responseCode==200){
// System.out.println("respondcode===> " + responseCode);
end = System.currentTimeMillis();
//Calculating the response time
difference = (end - start);
difference = difference / 1000;
// System.out.println("Response Time===> " + difference);
strbufstatus.append("Rsponse time===> " + difference + "<br>");
}
} catch (IOException ex) {
if (ex.toString().contains("java.net.UnknownHostException:")) {
strbufstatus.append(" - UnknownHostException has occured during Httpconnection\n");
} else if (ex.toString().contains("java.net.MalformedURLException: unknown protocol:")) {
strbufstatus.append(" - Unknown Protocol\n");
} else if (ex.toString().contains("java.net.ConnectException: Connection timed out: connect")) {
strbufstatus.append("Connection TimedOut\n");
} else {
strbufstatus.append("IOException has occured during Httpconnection \n");
}
ex.printStackTrace();
}
System.out.println("Status" +strbufstatus);
return strbufstatus.toString();
}
Upvotes: 0
Views: 1797
Reputation: 2340
One possibility is that the URL contains some characters that are not valid in a URL, for instance space or something outside of the ASCII range. This needs to be converted in some way to be sent as a URL, for instance as %HH escape sequences. Browsers do this automatically when you type in the address, but they don't all do it the same way. For instance, some use the now recommended convention of converting to UTF-8 sequences, but some use ISO-8859-1. Some convert space to + and some to %20. If you want your program to handle the URL the same way as the browser, you have to do the right encoding in your Java code.
Upvotes: 0
Reputation: 114817
From wikipedia on Error code 404:
The 404 or Not Found error message is a HTTP standard response code indicating that the client was able to communicate with the server, but the server could not find what was requested.
There should be a human readable "reason phrase" explaining what hasn't been found. You're just reading the response code - read and print in addition:
String responseMessage = httpUrlConnection.responseMessage();
Upvotes: 1
Reputation: 240946
Following can be issues :
Upvotes: 5