Here is my code
invoke(String url){
HttpURLConnection urlCon = null;
urlCon = (HttpURLConnection) new URL(url).openConnection();
urlCon.connect();
return urlCon.getResponseCode();
}
**"Unexpected end of file from server"**
I got error message like this when i try to pass the url of the jsp file like this (http://kart3:7890/sample/sample.jsp)
can u tell me am i miss anything
Working with httpurlconnection is a little more complicated than in your example. This is a nice java tutorial that can help: http://download.oracle.com/javase/tutorial/networking/urls/readingWriting.html
Related
I am using core java to retrieve single web page content as String using proxy.
private static HttpURLConnection getConnection(String urlStr) throws MalformedURLException, IOException {
URL url = new URL(urlStr);
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("216.56.48.118", 9000));
HttpURLConnection uc = (HttpURLConnection) url.openConnection(proxy);
return uc;
}
public static void printHomePageContent() throws Exception {
String queryParam = URLEncoder.encode(
"YBuD64pgMuP3hiqNmR4hB6H8xdAebeBPfdEPbdNUq3ptkbhSYGkdhuKaPCXE+KXT3unjfaI3tRJzQno10f/FiC7IzNAdhbrPK9d4smyxHpE=",
"UTF-8");
HttpURLConnection conn = getConnection(
"https://www.example.com?params=" + queryParam);
conn.setRequestMethod("GET");
conn.setReadTimeout(60 * 1000);
conn.setRequestProperty("Accept-Charset", "UTF-8");
conn.connect();
Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
for (int c; (c = in.read()) >= 0;) {
System.out.print((char) c);
}
}
NOTE: www.example.com is replaced with the actual url as I can't share it in public, all other things are exactly same as my original code.
PROBLEM: When I call printHomePageContent it's printing wrong page(same page if I don't send query param) content that means it's not considering params query parameter value as expected. While if If I hit the same URL on browser or POSTMAN(Rest Client), it's displaying right page. I am using proxy from browser as well using chrome extension.
I know you can't replicate the issue your own as I have replaced the URL, but the description I wrote is exactly what is happening. If anybody can suggest some hints based on their past experienced would be helpful.
Thanks in advance.
I'm trying to get a redirected URL from newURLConnection, but when I print out, it still outputs what the URL is before it is redirected. I'm certain that the original newURLConnection is a legitimate URL because when I enter it into a web browser it redirects automatically. I've tried it with conn.setInstanceFollowRedirects set to both false and true, and neither works...for some reason it's just impossible for me to get the redirected URL. Help would definitely be appreciated.
//open a connection
URLConnection newURLConnection = params[0].openConnection();
HttpURLConnection conn = (HttpURLConnection) newURLConnection;
//following code sourced from mkyong.com
conn.setInstanceFollowRedirects(true);
//HttpURLConnection.setFollowRedirects(true);
conn.connect();
//get the redirected URL
InputStream newURLConnectedStream = conn.getInputStream();
System.out.println("" + conn.getURL());
Try the following code to get it,
URL url = new URL(url);
HttpURLConnection ucon = (HttpURLConnection) url.openConnection();
ucon.setInstanceFollowRedirects(false);
URL secondURL = new URL(ucon.getHeaderField("Location"));
URLConnection conn = secondURL.openConnection();
Copied from java, android, resolve an url, get redirected uri
I am newbie developer in Java. STEP 1 I have already done the the following:
Logged in to REST API server (with login&password)
Received a token in XML format which i parsed with SAX parser so now i
am in a position of a token. Below is the sample code for Login:
Java code:
String url1 = "https://api4.liverail.com/login";
URL obj = new URL(url1);
HttpsURLConnection con1 = (HttpsURLConnection) obj.openConnection();
String urlParameters ="username=paania#gmail.com&password=d372a15b714bd250e";
con1.setDoOutput(true);
con1.setRequestMethod("POST");
DataOutputStream wr = new DataOutputStream(con1.getOutputStream());
wr.writeBytes(urlParameters);
STEP 2: I want to pass the token to REST API to obtain some information e.g a list from category but when i send the request via GET method , i get a response in XML saying [CDATA[You need to be logged in]] This is the code in Java:
String url = "http://api4.liverail.com/advertising/category/list/?token="72938howdwoi";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(newInputStreamReader(con.getInputStream()));
in.close();
con.disconnect();
I am not sure what i am missing here.
Any suggestions?
Just changed your url for request of data :
String url = "http://api4.liverail.com/advertising/category/list/?token=72938howdwoi";
I have some working java code which does the following:
URL myUrl = new URL("http://localhost:8080/webservice?user=" + username + "&password=" + password + "&request=x");
HttpURLConnection myConnection = (HttpURLConnection) myUrl.openConnection();
myConnection.setRequestMethod("POST");
// code continues to read the response stream
However, I noticed that my webserver access log contained the plaintext password for all of the users who connected. I would like to get this out of the access log, but the webserver admins claim that this needs to be changed in my code and not via webserver config.
I tried changing the code to the following:
URL myUrl = new URL("http://localhost:8080/webservice");
HttpURLConnection myConnection = (HttpURLConnection) myUrl.openConnection();
myConnection.setRequestMethod("POST");
// start of new code
myConnection.setDoOutput(true);
myConnection.addRequestProperty("username", username);
myConnection.addRequestProperty("password", password);
myConnection.addRequestProperty("request", "x");
// code continues to read the response stream
Now the access log does not contain the username/password/request method. However, the webservice now throws an exception indicating that it didn't receive any username/password.
What did I do wrong in my client code? I also tried using "setRequestProperty" instead of "addRequestProperty" and it had the same broken behavior.
I actually found the answer in another question on stackoverflow.
The correct code should be:
URL myUrl = new URL("http://localhost:8080/webservice");
HttpURLConnection myConnection = (HttpURLConnection) myUrl.openConnection();
myConnection.setRequestMethod("POST");
myConnection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(myConnection.getOutputStream ());
wr.writeBytes("username=" + username + "&password="+password + "&request=x");
// code continues to read the response stream
This is my code:
String httpURL = "http://codespeed_server:8000/result/add/";
URL myurl = new URL(httpURL);
HttpURLConnection con = (HttpURLConnection) myurl.openConnection();
con.setDoOutput(true);
DataOutputStream output = new DataOutputStream(con.getOutputStream());
output.writeBytes(query);
Can anybody tell me why this only works when I trail to it:
con.getResponseCode();
? The server only gets new entries when I call getResponseCode(). Is it a normal behavior ? Or is it a server-side issue ?
The URLConnection does not make a connection until one of the getResponse*, getInputStream methods (or other method which requires response data) is called (or connect()).