List of usage examples for java.net URLConnection getHeaderField
public String getHeaderField(int n)
From source file:Main.java
public static void main(String args[]) throws Exception { URL url = new URL("http://www.google.com"); URLConnection httpCon = (URLConnection) url.openConnection(); String s = httpCon.getHeaderField("Set-Cookie"); System.out.println(s);//from ww w. jav a2 s .c o m }
From source file:Main.java
public static void main(String[] argv) throws Exception { URL url = new URL("http://java2s.com"); URLConnection conn = url.openConnection(); for (int i = 0;; i++) { String headerName = conn.getHeaderFieldKey(i); String headerValue = conn.getHeaderField(i); System.out.println(headerName); System.out.println(headerValue); if (headerName == null && headerValue == null) { System.out.println("No more headers"); break; }//from w w w. j a va 2 s. c om } }
From source file:Main.java
public static void main(String[] argv) throws Exception { URL url = new URL("http://hostname:80"); URLConnection conn = url.openConnection(); for (int i = 0;; i++) { String headerName = conn.getHeaderFieldKey(i); String headerValue = conn.getHeaderField(i); System.out.println(headerName); System.out.println(headerValue); if (headerName == null && headerValue == null) { System.out.println("No more headers"); break; }/*from w w w . ja v a 2 s . co m*/ } }
From source file:Main.java
public static void main(String[] args) throws Exception { String s;/* www. j a v a 2 s. co m*/ s = "http://www.y.com/authTest"; URL url = new URL(s); URLConnection urlc = url.openConnection(); Map<String, List<String>> hf = urlc.getHeaderFields(); for (String key : hf.keySet()) System.out.println(key + ": " + urlc.getHeaderField(key)); System.out.println(((HttpURLConnection) urlc).getResponseCode()); }
From source file:Main.java
public static void main(String[] argv) throws Exception { URL url = new URL("http://hostname:80"); URLConnection conn = url.openConnection(); for (int i = 0;; i++) { String headerName = conn.getHeaderFieldKey(i); String headerValue = conn.getHeaderField(i); if (headerName == null && headerValue == null) { break; }//from w w w . j a v a2 s .c o m if ("Set-Cookie".equalsIgnoreCase(headerName)) { String[] fields = headerValue.split(";\\s*"); for (int j = 1; j < fields.length; j++) { if ("secure".equalsIgnoreCase(fields[j])) { System.out.println("secure=true"); } else if (fields[j].indexOf('=') > 0) { String[] f = fields[j].split("="); if ("expires".equalsIgnoreCase(f[0])) { System.out.println("expires" + f[1]); } else if ("domain".equalsIgnoreCase(f[0])) { System.out.println("domain" + f[1]); } else if ("path".equalsIgnoreCase(f[0])) { System.out.println("path" + f[1]); } } } } } }
From source file:MainClass.java
public static void main(String args[]) { URL u;/*from w w w .j a v a 2 s.c o m*/ URLConnection uc; String header; try { u = new URL("http://www.java2s.com"); uc = u.openConnection(); for (int j = 1;; j++) { header = uc.getHeaderField(j); if (header == null) break; System.out.println(uc.getHeaderFieldKey(j) + " " + header); } } catch (MalformedURLException e) { System.err.println("not a URL I understand."); } catch (IOException e) { System.err.println(e); } }
From source file:Main.java
public static void main(String args[]) throws Exception { String sessionCookie = null;/*ww w . ja v a 2 s. co m*/ URL url = new java.net.URL("http://127.0.0.1/yourServlet"); URLConnection con = url.openConnection(); if (sessionCookie != null) { con.setRequestProperty("cookie", sessionCookie); } con.setUseCaches(false); con.setDoOutput(true); con.setDoInput(true); ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(byteOut); out.flush(); byte buf[] = byteOut.toByteArray(); con.setRequestProperty("Content-type", "application/octet-stream"); con.setRequestProperty("Content-length", "" + buf.length); DataOutputStream dataOut = new DataOutputStream(con.getOutputStream()); dataOut.write(buf); dataOut.flush(); dataOut.close(); DataInputStream in = new DataInputStream(con.getInputStream()); int count = in.readInt(); in.close(); if (sessionCookie == null) { String cookie = con.getHeaderField("set-cookie"); if (cookie != null) { sessionCookie = parseCookie(cookie); System.out.println("Setting session ID=" + sessionCookie); } } System.out.println(count); }
From source file:Main.java
private static String getEncoding(URLConnection connection) { String contentTypeHeader = connection.getHeaderField("Content-Type"); if (contentTypeHeader != null) { int charsetStart = contentTypeHeader.indexOf("charset="); if (charsetStart >= 0) { return contentTypeHeader.substring(charsetStart + "charset=".length()); }/*from w w w. ja va 2 s. c o m*/ } return "UTF-8"; }
From source file:utils.ConnectionUtil.java
public static String getRealFileName(URL url) throws IOException { String fileName;//w w w. j av a2 s . c o m URLConnection conn = url.openConnection(); String raw = conn.getHeaderField("Content-Disposition"); // raw = "attachment; filename=abc.jpg" if (raw != null && raw.indexOf('=') != -1) { fileName = raw.split("=")[1]; //getting value after '=' } else { // fall back to random generated file name? String fileNameURL = url.getFile(); if (fileNameURL.lastIndexOf('?') != -1) { fileName = fileNameURL.substring(fileNameURL.lastIndexOf('/') + 1, fileNameURL.lastIndexOf('?')); } else { fileName = fileNameURL.substring(fileNameURL.lastIndexOf('/') + 1); } String contentType = conn.getHeaderField("Content-type"); if (contentType != null && !contentType.equals("") && !contentType.equals("application/octet-stream")) { try { String type = contentType.substring(contentType.indexOf('/') + 1, contentType.indexOf(';')); if (type != null && !type.equals("")) { fileName = FilenameUtils.getBaseName(fileName) + '.' + type; } } catch (Exception e) { } } } return fileName; }
From source file:Urls.java
/** * Returns the time when the document should be considered expired. * The time will be zero if the document always needs to be revalidated. * It will be <code>null</code> if no expiration time is specified. *//* w w w .j ava2s .c o m*/ public static Long getExpiration(URLConnection connection, long baseTime) { String cacheControl = connection.getHeaderField("Cache-Control"); if (cacheControl != null) { StringTokenizer tok = new StringTokenizer(cacheControl, ","); while (tok.hasMoreTokens()) { String token = tok.nextToken().trim().toLowerCase(); if ("must-revalidate".equals(token)) { return new Long(0); } else if (token.startsWith("max-age")) { int eqIdx = token.indexOf('='); if (eqIdx != -1) { String value = token.substring(eqIdx + 1).trim(); int seconds; try { seconds = Integer.parseInt(value); return new Long(baseTime + seconds * 1000); } catch (NumberFormatException nfe) { logger.warning("getExpiration(): Bad Cache-Control max-age value: " + value); // ignore } } } } } String expires = connection.getHeaderField("Expires"); if (expires != null) { try { synchronized (PATTERN_RFC1123) { Date expDate = PATTERN_RFC1123.parse(expires); return new Long(expDate.getTime()); } } catch (java.text.ParseException pe) { int seconds; try { seconds = Integer.parseInt(expires); return new Long(baseTime + seconds * 1000); } catch (NumberFormatException nfe) { logger.warning("getExpiration(): Bad Expires header value: " + expires); } } } return null; }