import java.net.*;
import java.io.*;
import java.util.*;
public class CookieSplitter {
private static final String cookieName = "Name of cookie to find";
public static void proxy(){
// if you need to set up a proxy for your connection
Properties systemProperties = System.getProperties();
systemProperties.setProperty("http.proxyHost","your proxy");
systemProperties.setProperty("http.proxyPort","port to connect to on the proxy");
}
public static void main(String[] args){
try{
// if you use a proxy
proxy();
String[] cookies = null;
// just grabbing the url from the args for this example
URL url = new URL(args[0]);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.connect();
InputStream in = connection.getInputStream();
Iterator iterator = connection.getHeaderFields().entrySet().iterator();
if(iterator != null){
while(iterator.hasNext()){
String headerString = iterator.next().toString();
int beginIndex;
if((beginIndex = headerString.indexOf(cookieName)) != -1){
// usually cookies take the form name=value;
// so to get the value for this cookie move the beginIndex along
// cookieName.length()+1 spaces
String s = headerString.substring(beginIndex+cookieName.length()+1);
// and split this new string on the first ';' in case there
// is more than one cookie in the header
s = s.split(";")[0];
// now s will be the "Herb,/docs/sample/file/" given in your example
// so to split that it's a simple
cookies = s.split(",");
break;
}
}
}
if(cookies != null && cookies.length == 2){
// use the 2 cookies you were looking for!
}
in.close();
connection.disconnect();
}catch(MalformedURLException mfuex){
mfuex.printStackTrace();
}catch(IOException ioex){
ioex.printStackTrace();
}
}
}