Skip to content

Instantly share code, notes, and snippets.

@FloydHsiu
Created June 11, 2016 16:28
Show Gist options
  • Save FloydHsiu/a9d55eb5d012ec64b4d1442680c90712 to your computer and use it in GitHub Desktop.
Save FloydHsiu/a9d55eb5d012ec64b4d1442680c90712 to your computer and use it in GitHub Desktop.
static public void getCookie() throws IOException
{
HttpURLConnection HttpConn;
URL Url = new URL(INDEX_URL);
HttpConn = (HttpURLConnection) Url.openConnection();
System.out.println(HttpConn);
HttpConn.setConnectTimeout(15000);
HttpConn.setReadTimeout(10000);
HttpConn.setRequestMethod("GET");
HttpConn.setDoInput(true);
HttpConn.setDoOutput(false);
//start the query
HttpConn.connect();
int response = HttpConn.getResponseCode();//http 200,403 ......
if(response == 200)
{
String Session_header = HttpConn.getHeaderField("Set-Cookie");
if(Session_header != null)
{
cookieManager.getCookieStore().add(null, HttpCookie.parse(Session_header).get(0));
}
}
else
{
System.out.println("GG");
throw new IOException();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment