Java: HTTPS URL not redirecting - java

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

Related

How to make REST API call using a token?

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";

HttpUrlConnection addRequestProperty Method Not Passing Parameters

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

Java request only works when

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()).

Picasa getAlbum request not working Android

String album = "http://picasaweb.google.com/data/feed/api/user/"+email;
HttpURLConnection con = (HttpURLConnection) new URL(albumUrl).openConnection();
// request method, timeout and headers
con.setRequestMethod("GET") ;
con.setReadTimeout(15000);
con.setRequestProperty("Authorization", "GoogleLogin auth="+auth);
con.setRequestProperty("GData-Version", "2");
// set timeout and that we will process output
con.setReadTimeout(15000);
con.setDoOutput(true);
// connnect to url
con.connect();
// read output returned for url
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
Problem : Everytime i call con.getInputStream() it gives me file not found exception.
But when i load the same url in the desktop browser then it is displaying correct data.
I am confused why on android it is throwing exception.
Thanks in advance.
Did you get this? Maybe you just missed the https
below example uses default for authenticated user and the experimental fields list.
url = "https://picasaweb.google.com/data/feed/api/user/default?kind=album&access=public&fields="
+ URLEncoder
.encode("entry(title,id,gphoto:numphotosremaining,gphoto:numphotos,media:group/media:thumbnail)",
"UTF-8");
https://developers.google.com/picasa-web/docs/2.0/developers_guide_protocol#ListAlbums

Unexpected end of file from server

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

Categories

Resources