0

I am trying to use the following code to get the redirected URL and then do some processing on it. But when I print the redirected link, it goes to a page that informs the absence of cookies. how is it possible to enable cookies while opening the url's connection?

 String url = "http://dx.doi.org/10.1137/0210059";
 URLConnection con = new URL( url ).openConnection();
 con.getInputStream();
 String redirctedURL= con.getURL().toString();
 System.out.println(redirctedURL);
lonesome
  • 2,503
  • 6
  • 35
  • 61
  • The problem is how to handle cookies. Please look at this [thread](http://stackoverflow.com/questions/6354294/urlconnection-with-cookies) – Scar Jan 22 '16 at 08:10

1 Answers1

0

When using java UrlConnection's, you should handle the cookies by yourself, to read and set the cookies, you can use the setRequestProperty() and getHeaderField() of URLConnection.

The remaining part is parsing the cookies by yourself, an example how the can be done is as follows:

Map<String, String> cookie = new HashMap<>();
public URLConnection doConnctionWithCookies(URL url) {
    StringBuilder builder = new StringBuilder();
    builder.append("&");
    for(Map.Entry<String,String> entry : cookie.entrySet()) {
        builder.append(urlenEncode(entry.getKey()))
               .append("=")
               .append(urlenEncode(entry.getValue()))
               .append("&");
    }
    builder.setLength(builder.length() - 1);
    URLConnection con = url.openConnection();
    con.setRequestProperty("Cookie", builder.toString());
    con.connect();
    // Parse cookie headers
    List<String> setCookie = con.getHeaderFields().get("set-cookie");
    // HTTP Spec state header fields MUST be case INsentive, I expet the above to work with all servers 
    if(setCookie == null)
        return con;
    // Parse all the cookies
    for (String str : setCookie) {
        String[] cookieKeyValue = str.split(";")[0].split("=",2);
        if (cookieKeyValue.length != 2) {
            continue;
        }
        cookie.put(urlenDecode(cookieKeyValue[0]), urlenDecode(cookieKeyValue[1]));
    }
    return con;
}

public String urlenEncode(String en) {
    return URLEncoder.encode(en, "UTF-8");
}

public String urlenDecode(String en) {
    return URLDecoder.decode(en, "UTF-8");
}

The above implementation is a very stupid and crude way of implementation cookies, while it works, it totally ignores the fact that cookies can also have a host parameter to prevent identification cookies be shared across multiple hosts.

A better way than doing it yourself can be using a library dedicated for the task like Apache HttpClient.

Ferrybig
  • 18,194
  • 6
  • 57
  • 79