List of usage examples for java.net URLConnection getHeaderFields
public Map<String, List<String>> getHeaderFields()
From source file:Main.java
public static void main(String[] args) throws Exception { URL url = new URL("http://www.google.com/index.html"); URLConnection connection = url.openConnection(); Map responseMap = connection.getHeaderFields(); for (Iterator iterator = responseMap.keySet().iterator(); iterator.hasNext();) { String key = (String) iterator.next(); System.out.println(key + " = "); List values = (List) responseMap.get(key); for (int i = 0; i < values.size(); i++) { Object o = values.get(i); System.out.println(o + ", "); }// ww w . j a va 2 s. co m } }
From source file:Main.java
public static void main(String[] args) throws Exception { String s;// ww w .j ava2 s .c om 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[] args) throws Exception { URL url = new URL("http://www.java2s.com/"); URLConnection urlConnection = url.openConnection(); Map<String, List<String>> headers = urlConnection.getHeaderFields(); Set<Map.Entry<String, List<String>>> entrySet = headers.entrySet(); for (Map.Entry<String, List<String>> entry : entrySet) { String headerName = entry.getKey(); System.out.println("Header Name:" + headerName); List<String> headerValues = entry.getValue(); for (String value : headerValues) { System.out.print("Header value:" + value); }//from w w w . java2s. c o m System.out.println(); } }
From source file:MainClass.java
public static void main(String[] args) { try {/* www . ja v a 2 s . co m*/ URL url = new URL("http://www.java2s.com/"); URLConnection urlConnection = url.openConnection(); Map<String, List<String>> headers = urlConnection.getHeaderFields(); Set<Map.Entry<String, List<String>>> entrySet = headers.entrySet(); for (Map.Entry<String, List<String>> entry : entrySet) { String headerName = entry.getKey(); System.out.println("Header Name:" + headerName); List<String> headerValues = entry.getValue(); for (String value : headerValues) { System.out.print("Header value:" + value); } System.out.println(); System.out.println(); } InputStream inputStream = urlConnection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String line = bufferedReader.readLine(); while (line != null) { System.out.println(line); line = bufferedReader.readLine(); } bufferedReader.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:URLConnectionTest.java
public static void main(String[] args) { try {/*from ww w . j ava 2s .co m*/ String urlName; if (args.length > 0) urlName = args[0]; else urlName = "http://java.sun.com"; URL url = new URL(urlName); URLConnection connection = url.openConnection(); // set username, password if specified on command line if (args.length > 2) { String username = args[1]; String password = args[2]; String input = username + ":" + password; String encoding = base64Encode(input); connection.setRequestProperty("Authorization", "Basic " + encoding); } connection.connect(); // print header fields Map<String, List<String>> headers = connection.getHeaderFields(); for (Map.Entry<String, List<String>> entry : headers.entrySet()) { String key = entry.getKey(); for (String value : entry.getValue()) System.out.println(key + ": " + value); } // print convenience functions System.out.println("----------"); System.out.println("getContentType: " + connection.getContentType()); System.out.println("getContentLength: " + connection.getContentLength()); System.out.println("getContentEncoding: " + connection.getContentEncoding()); System.out.println("getDate: " + connection.getDate()); System.out.println("getExpiration: " + connection.getExpiration()); System.out.println("getLastModifed: " + connection.getLastModified()); System.out.println("----------"); Scanner in = new Scanner(connection.getInputStream()); // print first ten lines of contents for (int n = 1; in.hasNextLine() && n <= 10; n++) System.out.println(in.nextLine()); if (in.hasNextLine()) System.out.println(". . ."); } catch (IOException e) { e.printStackTrace(); } }
From source file:Main.java
public static void printRequestHeaders(URLConnection connection) { Map headers = connection.getHeaderFields(); System.out.println(headers);//ww w . jav a2 s .c om }
From source file:com.ettoremastrogiacomo.sktradingjava.starters.Temp.java
public static void testCookies() throws Exception { URL url = new URL("http://www.repubblica.it"); URLConnection conn = url.openConnection(); //conn.setRequestProperty("Cookie", "name1=value1; name2=value2"); conn.connect();//w w w . ja v a 2 s .co m Map<String, List<String>> map = conn.getHeaderFields(); map.keySet().forEach((s) -> { LOG.debug(s + "\t" + map.get(s)); }); }
From source file:com.moviejukebox.themoviedb.tools.WebBrowser.java
private static void readHeader(URLConnection cnx) { // read new cookies and update our cookies for (Map.Entry<String, List<String>> header : cnx.getHeaderFields().entrySet()) { if ("Set-Cookie".equals(header.getKey())) { for (String cookieHeader : header.getValue()) { String[] cookieElements = cookieHeader.split(" *; *"); if (cookieElements.length >= 1) { String[] firstElem = cookieElements[0].split(" *= *"); String cookieName = firstElem[0]; String cookieValue = firstElem.length > 1 ? firstElem[1] : null; String cookieDomain = null; // find cookie domain for (int i = 1; i < cookieElements.length; i++) { String[] cookieElement = cookieElements[i].split(" *= *"); if ("domain".equals(cookieElement[0])) { cookieDomain = cookieElement.length > 1 ? cookieElement[1] : null; break; }/*w w w . ja va 2s.c o m*/ } if (cookieDomain == null) { // if domain isn't set take current host cookieDomain = cnx.getURL().getHost(); } Map<String, String> domainCookies = cookies.get(cookieDomain); if (domainCookies == null) { domainCookies = new HashMap<String, String>(); cookies.put(cookieDomain, domainCookies); } // add or replace cookie domainCookies.put(cookieName, cookieValue); } } } } }
From source file:imageLines.ImageHelpers.java
public static String getCookie(String urlString) throws MalformedURLException, IOException { URL url = new URL(urlString); URLConnection conn = url.openConnection(); conn.connect();//from w w w . jav a2s . c om Map<String, List<String>> headers = conn.getHeaderFields(); List<String> values = headers.get("Set-Cookie"); String cookieValue = null; for (Iterator iter = values.iterator(); iter.hasNext();) { String v = iter.next().toString(); return v; } return ""; }
From source file:org.n52.wps.test.AllTestsIT.java
public static void checkContentDispositionOfRetrieveResultServlet(String response, String filename, String suffix) throws IOException, ParserConfigurationException, SAXException { String refResult = AllTestsIT.getAsyncDoc(response); ExecuteResponseDocument document = null; try {/*from ww w. j a v a2s . co m*/ document = ExecuteResponseDocument.Factory.parse(refResult); } catch (Exception e) { System.err.println("Could not parse execute response document."); } assertThat(document, not(nullValue())); ProcessOutputs outputs = document.getExecuteResponse().getProcessOutputs(); assertThat(outputs, not(nullValue())); assertThat(outputs.sizeOfOutputArray(), not(0)); OutputDataType outputDataType = document.getExecuteResponse().getProcessOutputs().getOutputArray(0); assertThat(outputDataType, not(nullValue())); OutputReferenceType data = outputDataType.getReference(); assertThat(data, not(nullValue())); String url = data.getHref(); if (filename != null) { //concat filename to URL url = url.concat("&filename=" + filename); } URLConnection urlConnection = new URL(url).openConnection(); List<String> headerFields = urlConnection.getHeaderFields().get("Content-Disposition"); boolean oneHeaderFieldContainsFilename = false; for (String field : headerFields) { if (field.contains("filename")) { oneHeaderFieldContainsFilename = true; if (suffix != null && !suffix.equals("")) { assertTrue(field.endsWith(suffix + "\"")); } if (filename != null && !filename.equals("")) { assertTrue(field.contains(filename)); } } } assertTrue(oneHeaderFieldContainsFilename); }