List of usage examples for java.net URLConnection getHeaderFields
public Map<String, List<String>> getHeaderFields()
From source file:com.omertron.themoviedbapi.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; }//from w w w. j av a2s . c om } 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:com.omertron.thetvdbapi.tools.WebBrowser.java
/** * Read the header information into the cookies * * @param cnx//from ww w . j a va2 s. c o m */ 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; } } 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:com.connectsdk.core.upnp.Device.java
public static Device createInstanceFromXML(String url, String searchTarget) { Device newDevice = null;/*from w ww . j av a 2 s .c o m*/ try { newDevice = new Device(url, searchTarget); } catch (IOException e) { return null; } final Device device = newDevice; DefaultHandler dh = new DefaultHandler() { String currentValue = null; Icon currentIcon; Service currentService; @Override public void characters(char[] ch, int start, int length) throws SAXException { currentValue = new String(ch, start, length); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (Icon.TAG.equals(qName)) { currentIcon = new Icon(); } else if (Service.TAG.equals(qName)) { currentService = new Service(); currentService.baseURL = device.baseURL; } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { // System.out.println("[DEBUG] qName: " + qName + ", currentValue: " + currentValue); /* Parse device-specific information */ if (TAG_DEVICE_TYPE.equals(qName)) { device.deviceType = currentValue; } else if (TAG_FRIENDLY_NAME.equals(qName)) { device.friendlyName = currentValue; } else if (TAG_MANUFACTURER.equals(qName)) { device.manufacturer = currentValue; } else if (TAG_MANUFACTURER_URL.equals(qName)) { device.manufacturerURL = currentValue; } else if (TAG_MODEL_DESCRIPTION.equals(qName)) { device.modelDescription = currentValue; } else if (TAG_MODEL_NAME.equals(qName)) { device.modelName = currentValue; } else if (TAG_MODEL_NUMBER.equals(qName)) { device.modelNumber = currentValue; } else if (TAG_MODEL_URL.equals(qName)) { device.modelURL = currentValue; } else if (TAG_SERIAL_NUMBER.equals(qName)) { device.serialNumber = currentValue; } else if (TAG_UDN.equals(qName)) { device.UDN = currentValue; // device.UUID = Device.parseUUID(currentValue); } else if (TAG_UPC.equals(qName)) { device.UPC = currentValue; } /* Parse icon-list information */ else if (Icon.TAG_MIME_TYPE.equals(qName)) { currentIcon.mimetype = currentValue; } else if (Icon.TAG_WIDTH.equals(qName)) { currentIcon.width = currentValue; } else if (Icon.TAG_HEIGHT.equals(qName)) { currentIcon.height = currentValue; } else if (Icon.TAG_DEPTH.equals(qName)) { currentIcon.depth = currentValue; } else if (Icon.TAG_URL.equals(qName)) { currentIcon.url = currentValue; } else if (Icon.TAG.equals(qName)) { device.iconList.add(currentIcon); } /* Parse service-list information */ else if (Service.TAG_SERVICE_TYPE.equals(qName)) { currentService.serviceType = currentValue; } else if (Service.TAG_SERVICE_ID.equals(qName)) { currentService.serviceId = currentValue; } else if (Service.TAG_SCPD_URL.equals(qName)) { currentService.SCPDURL = currentValue; } else if (Service.TAG_CONTROL_URL.equals(qName)) { currentService.controlURL = currentValue; } else if (Service.TAG_EVENTSUB_URL.equals(qName)) { currentService.eventSubURL = currentValue; } else if (Service.TAG.equals(qName)) { device.serviceList.add(currentService); } } }; SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser; try { URL mURL = new URL(url); URLConnection urlConnection = mURL.openConnection(); InputStream in = new BufferedInputStream(urlConnection.getInputStream()); try { parser = factory.newSAXParser(); parser.parse(in, dh); } finally { in.close(); } device.headers = urlConnection.getHeaderFields(); return device; } catch (MalformedURLException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:tufts.vue.ds.XMLIngest.java
private static InputStream getTestXMLStream() throws IOException { // // SMF 2008-10-02: E.g. Craigslist XML streams use ISO-8859-1, which is provided in // // HTML headers as "Content-Type: application/rss+xml; charset=ISO-8859-1", (tho not // // in a special content-encoding header), and our current XML parser fails unless // // the stream is read with this set: e.g.: [org.xml.sax.SAXParseException: Character // // conversion error: "Unconvertible UTF-8 character beginning with 0x95" (line // // number may be too low).] Actually, in this case it turns out that providing a // // default InputStreamReader (encoding not specified) as opposed to a direct // // InputStream from the URLConnection works, and the XML parser is presumably then // // finding and handling the "<?xml version="1.0" encoding="ISO-8859-1"?>" line at // // the top of the XML stream // final XmlSchema schema = new XmlSchema(conn.getURL(), itemKey); // InputStream is = null; // try { // is = conn.getInputStream(); // errout("GOT INPUT STREAM: " + Util.tags(is)); // } catch (IOException e) { // e.printStackTrace(); // return null; // }//from w w w . ja v a 2 s . co m // final Document doc = parseXML(is, false); // Could also use a ROME API XmlReader(URLConnection) for handling // the input, which does it's own magic to figure out the encoding. // For more on the complexity of this issue, see: // http://diveintomark.org/archives/2004/02/13/xml-media-types URL url = new URL(JIRA_VUE_URL); URLConnection conn = url.openConnection(); conn.setRequestProperty("Cookie", JIRA_SFRAIZE_COOKIE); errout("Opening connection to " + url); conn.connect(); errout("Getting InputStream..."); InputStream in = conn.getInputStream(); errout("Got " + Util.tags(in)); errout("Getting headers..."); Map<String, List<String>> headers = conn.getHeaderFields(); errout("HEADERS:"); for (Map.Entry<String, List<String>> e : headers.entrySet()) { errout(e.getKey() + ": " + e.getValue()); } return in; }
From source file:CookieAccessor.java
/** * Retrieves cookie from header fields in URL connection. * Another way to get cookies before recent changes to CookieManager *///from ww w . ja v a2 s .co m public void getCookieFromURLConn() { try { URL url = new URL("http://host.example.com"); URLConnection conn = url.openConnection(); Map<String, List<String>> headers = conn.getHeaderFields(); // the literal "Set-Cookie" may be capitalized differently in different web sites List<String> values = headers.get("Set-cookie"); if (values != null) { String cookieValue = null; for (Iterator iter = values.iterator(); iter.hasNext();) { cookieValue = (String) iter.next(); System.out.println("Cookie value from URL connection: " + cookieValue); } } else { System.out.println("No cookies found"); } } catch (Exception e) { System.out.println("Unable to get cookie from URL connection"); e.printStackTrace(); } }
From source file:org.apache.jsp.fileUploader_jsp.java
public static String getConnectionInfiniteCookie(URLConnection urlConnection) { 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(); if (headerName != null && headerName.equals("Set-Cookie")) { List<String> headerValues = entry.getValue(); for (String value : headerValues) { if (value.contains("infinitecookie")) { int equalsLoc = value.indexOf("="); int semicolonLoc = value.indexOf(";"); //System.out.println("Got Connection Cookie Line 133: " + value.substring(equalsLoc+1,semicolonLoc)); return value.substring(equalsLoc + 1, semicolonLoc); }/*from ww w.j av a2s .c om*/ } } } return null; }
From source file:com.moviejukebox.tools.WebBrowser.java
private 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; }//from w w w . j a v a 2 s. c o m } if (cookieDomain == null) { // if domain isn't set take current host cookieDomain = cnx.getURL().getHost(); } putCookie(cookieDomain, cookieName, cookieValue); } } } } }
From source file:com.sastix.cms.client.impl.CmsClient.java
@Override public ResponseDTO getData(final String uri) throws IOException { final String url = apiVersionClient.getApiUrl() + "/" + Constants.GET_DATA + "/" + uri; URLConnection urlConnection = new URL(url).openConnection(); final Map<String, List<String>> headers = urlConnection.getHeaderFields(); final InputStream inputStream = urlConnection.getInputStream(); final Map<String, String> responseHeaders = new HashMap<>(); responseHeaders.put(ResponseDTO.CONTENT_TYPE, headers.get(ResponseDTO.CONTENT_TYPE).get(0)); return new ResponseDTO(inputStream, responseHeaders); }
From source file:com.sastix.cms.client.impl.CmsClient.java
@Override public ResponseDTO getDataFromUUID(final String uuid) throws IOException { final String url = apiVersionClient.getApiUrl() + "/" + Constants.GET_DATA_FROM_UUID + "/" + uuid; URLConnection urlConnection = new URL(url).openConnection(); final Map<String, List<String>> headers = urlConnection.getHeaderFields(); final InputStream inputStream = urlConnection.getInputStream(); final Map<String, String> responseHeaders = new HashMap<>(); responseHeaders.put(ResponseDTO.CONTENT_TYPE, headers.get(ResponseDTO.CONTENT_TYPE).get(0)); return new ResponseDTO(inputStream, responseHeaders); }
From source file:org.inaetics.pubsub.demo.config.EtcdWrapper.java
/** * Get a node from etcd. If wait is true it won't return until an etcd update happened. Cannot be * interrupted.//from w w w . j a v a 2 s. c o m * * @param key * @param wait * @param recursive * @param index * @return * @throws MalformedURLException * @throws IOException */ public JsonNode get(String key, boolean wait, boolean recursive, long index) throws MalformedURLException, IOException { String getUrl = url + key; if (wait || recursive) { // needs refactoring but works for now getUrl += "?"; if (wait) { getUrl += "wait=true"; if (index > 0) { getUrl += "&waitIndex=" + index; } } if (recursive) { if (wait) { getUrl += "&"; } getUrl += "recursive=true"; } } URLConnection connection = new URL(getUrl).openConnection(); InputStream response = connection.getInputStream(); JsonNode result = new ObjectMapper().readTree(response); response.close(); AddEtcdIndex(result, Long.parseLong(connection.getHeaderFields().get("X-Etcd-Index").get(0))); return result; }