List of usage examples for java.net URLConnection getHeaderFields
public Map<String, List<String>> getHeaderFields()
From source file:servlet.GetCity.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods.//from w w w .j a v a 2 s. co m * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ public String getData(String urlString) { String result = ""; BufferedReader read = null; try { URL realurl = new URL(urlString); URLConnection connection = realurl.openConnection(); connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); connection.connect(); Map<String, List<String>> map = connection.getHeaderFields(); for (String key : map.keySet()) { System.out.println(key + "--->" + map.get(key)); } read = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); String line; while ((line = read.readLine()) != null) { result += line; } } catch (IOException e) { e.printStackTrace(); } finally { if (read != null) { try { read.close(); } catch (Exception e) { e.printStackTrace(); } } } return result; }
From source file:com.wanikani.wklib.Connection.java
protected void measureHeaders(Meter meter, URLConnection conn, boolean clen) { Map<String, List<String>> hdrs; hdrs = conn.getHeaderFields(); if (hdrs == null) return;/*from w w w . j a va 2 s. co m*/ for (Map.Entry<String, List<String>> e : hdrs.entrySet()) { if (e.getKey() != null) meter.count(e.getKey().length() + 1); for (String s : e.getValue()) meter.count(s.length() + 3); if (clen && e.getKey() != null && e.getKey().equals("Content-Length") && !e.getValue().isEmpty()) { try { meter.count(Integer.parseInt(e.getValue().get(0))); } catch (NumberFormatException x) { /* empty */ } } } meter.sync(); }
From source file:org.apache.jsp.communities_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(";"); return value.substring(equalsLoc + 1, semicolonLoc); }// ww w. j av a2 s. com } } } return null; }
From source file:org.webical.dao.util.WebDavCalendarSynchronisation.java
/** * Retrieves last modification datetime of the specified calendar * * @param calendar - calendar to retrieve last modification datetime * @return date of last modification or null * * @throws DaoException//from w ww. ja v a 2 s . c om * @throws MalformedURLException * @throws IOException * @throws NoSuchAlgorithmException * @throws KeyManagementException */ public Date lastModificationCalendar(Calendar calendar) throws DaoException, MalformedURLException, IOException, NoSuchAlgorithmException, KeyManagementException { if (log.isDebugEnabled()) log.debug("lastModificationCalendar " + calendar.getName()); Date calLastMod = null; if (calendar == null || calendar.getUrl() == null) { throw new DaoException("calendar or url should not be null"); } // Set up authentication if needed if (calendar.getUsername() != null && calendar.getPassword() != null) { ConnectionUtil.setAuthentication(calendar.getUsername(), calendar.getPassword()); } // Get the correct input source URLConnection connection = null; URL url = new URL(calendar.getUrl()); if (url.getProtocol().equalsIgnoreCase(ConnectionUtil.HTTPS_PROTOCOL)) { //TODO get from User Settings connection = ConnectionUtil.getURLConnection(url, true); } else { connection = url.openConnection(); } log.debug(connection.getHeaderFields()); Date datum = new Date(connection.getLastModified()); // GMT date time if (datum.getTime() > 1000000000000L) calLastMod = datum; // > 09/09/2001 return calLastMod; }
From source file:com.android.tools.idea.sdk.remote.internal.UrlOpener.java
private static Pair<InputStream, HttpResponse> openWithUrl(String url, Header[] inHeaders) throws IOException { URL u = new URL(url); URLConnection c = u.openConnection(); c.setConnectTimeout(sConnectionTimeoutMs); c.setReadTimeout(sSocketTimeoutMs);/*from w w w. j a v a2s .c om*/ if (inHeaders != null) { for (Header header : inHeaders) { c.setRequestProperty(header.getName(), header.getValue()); } } // Trigger the access to the resource // (at which point setRequestProperty can't be used anymore.) int code = 200; if (c instanceof HttpURLConnection) { code = ((HttpURLConnection) c).getResponseCode(); } // Get the input stream. That can fail for a file:// that doesn't exist // in which case we set the response code to 404. // Also we need a buffered input stream since the caller need to use is.reset(). InputStream is = null; try { is = new BufferedInputStream(c.getInputStream()); } catch (Exception ignore) { if (is == null && code == 200) { code = 404; } } HttpResponse outResponse = new BasicHttpResponse(new ProtocolVersion(u.getProtocol(), 1, 0), // make up the protocol version code, ""); //$NON-NLS-1$; Map<String, List<String>> outHeaderMap = c.getHeaderFields(); for (Entry<String, List<String>> entry : outHeaderMap.entrySet()) { String name = entry.getKey(); if (name != null) { List<String> values = entry.getValue(); if (!values.isEmpty()) { outResponse.setHeader(name, values.get(0)); } } } return Pair.of(is, outResponse); }
From source file:org.infoglue.igide.helper.http.HTTPTextDocumentListenerEngine.java
private void listen() { String errorMessage;/*from ww w.j a v a 2 s .c o m*/ boolean error; errorMessage = ""; error = false; try { Logger.logConsole("Starting listen thread"); URLConnection urlConn = url.openConnection(); urlConn.setConnectTimeout(3000); urlConn.setRequestProperty("Connection", "Keep-Alive"); urlConn.setReadTimeout(0); urlConn.setDoInput(true); urlConn.setDoOutput(true); urlConn.setUseCaches(false); urlConn.setAllowUserInteraction(false); if (urlConn.getHeaderFields().toString().indexOf("401 Unauthorized") > -1) { Logger.logConsole("User has no access to the CMS - closing connection"); throw new AccessControlException("User has no access to the CMS - closing connection"); } String boundary = urlConn.getHeaderField("boundary"); DataInputStream input = new DataInputStream(urlConn.getInputStream()); StringBuffer buf = new StringBuffer(); if (listener != null) listener.onConnection(url); String str = null; while ((str = input.readLine()) != null) { if (str.indexOf("XMLNotificationWriter.ping") == -1) { if (str.equals(boundary)) { String message = buf.toString(); // By checking there is more in the String than the XML declaration we assume the message is valid if (message != null && !message.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "").equals("")) { if (listener != null) listener.recieveDocument(buf.toString()); else Logger.logConsole((new StringBuilder("NEW DOCUMENT!!\r\n")).append(buf.toString()) .append("\r\n").toString()); } buf = new StringBuffer(); } else { buf.append(str); } } } input.close(); } catch (MalformedURLException me) { error = true; errorMessage = (new StringBuilder("Faulty CMS-url:")).append(url).toString(); if (listener != null) listener.onException(me); else System.err.println((new StringBuilder("MalformedURLException: ")).append(me).toString()); final String errorMessageFinal = errorMessage; Logger.logConsole( (new StringBuilder("The connection was shut. Was it an error:")).append(error).toString()); if (!error) { if (System.currentTimeMillis() - lastRetry > 20000L) { Logger.logConsole("Trying to restart the listener as it was a while since last..."); lastRetry = System.currentTimeMillis(); listen(); } } else { try { if (listener != null) listener.onEndConnection(url); } catch (Exception e) { Logger.logConsole( (new StringBuilder("Error ending connection:")).append(e.getMessage()).toString()); } Display.getDefault().asyncExec(new Runnable() { public void run() { MessageDialog.openError(viewer.getControl().getShell(), "Error", (new StringBuilder()).append(errorMessageFinal).toString()); } }); } } catch (IOException ioe) { error = true; errorMessage = "Got an I/O-Exception talking to the CMS. Check that it is started and in valid state."; Logger.logConsole((new StringBuilder("ioe: ")).append(ioe.getMessage()).toString()); if (listener != null) listener.onException(ioe); else Logger.logConsole((new StringBuilder("TextDocumentListener cannot connect to: ")) .append(url.toExternalForm()).toString()); final String errorMessageFinal = errorMessage; Logger.logConsole( (new StringBuilder("The connection was shut. Was it an error:")).append(error).toString()); if (!error) { if (System.currentTimeMillis() - lastRetry > 20000L) { Logger.logConsole("Trying to restart the listener as it was a while since last..."); lastRetry = System.currentTimeMillis(); listen(); } } else { try { if (listener != null) listener.onEndConnection(url); } catch (Exception e) { Logger.logConsole( (new StringBuilder("Error ending connection:")).append(e.getMessage()).toString()); } Display.getDefault().asyncExec(new Runnable() { public void run() { MessageDialog.openError(viewer.getControl().getShell(), "Error", (new StringBuilder()).append(errorMessageFinal).toString()); } }); } } catch (AccessControlException ace) { error = true; errorMessage = "The user you tried to connect with did not have the correct access rights. Check that he/she has roles etc enough to access the CMS"; Logger.logConsole((new StringBuilder("ioe: ")).append(ace.getMessage()).toString()); if (listener != null) listener.onException(ace); else Logger.logConsole((new StringBuilder()).append(ace.getMessage()).toString()); final String errorMessageFinal = errorMessage; Logger.logConsole( (new StringBuilder("The connection was shut. Was it an error:")).append(error).toString()); if (!error) { if (System.currentTimeMillis() - lastRetry > 20000L) { Logger.logConsole("Trying to restart the listener as it was a while since last..."); lastRetry = System.currentTimeMillis(); listen(); } } else { try { if (listener != null) listener.onEndConnection(url); } catch (Exception e) { Logger.logConsole( (new StringBuilder("Error ending connection:")).append(e.getMessage()).toString()); } Display.getDefault().asyncExec(new Runnable() { public void run() { MessageDialog.openError(viewer.getControl().getShell(), "Error", (new StringBuilder()).append(errorMessageFinal).toString()); } }); } } catch (Exception exception) { final String errorMessageFinal = errorMessage; Logger.logConsole( (new StringBuilder("The connection was shut. Was it an error:")).append(error).toString()); if (!error) { if (System.currentTimeMillis() - lastRetry > 20000L) { Logger.logConsole("Trying to restart the listener as it was a while since last..."); lastRetry = System.currentTimeMillis(); listen(); } } else { try { if (listener != null) listener.onEndConnection(url); } catch (Exception e) { Logger.logConsole( (new StringBuilder("Error ending connection:")).append(e.getMessage()).toString()); } Display.getDefault().asyncExec(new Runnable() { public void run() { MessageDialog.openError(viewer.getControl().getShell(), "Error", (new StringBuilder()).append(errorMessageFinal).toString()); } }); } } catch (Throwable e) { final String errorMessageFinal = errorMessage; Logger.logConsole( (new StringBuilder("The connection was shut. Was it an error:")).append(error).toString()); if (!error) { if (System.currentTimeMillis() - lastRetry > 20000L) { Logger.logConsole("Trying to restart the listener as it was a while since last..."); lastRetry = System.currentTimeMillis(); listen(); } } else { try { if (listener != null) listener.onEndConnection(url); } catch (Exception e2) { Logger.logConsole( (new StringBuilder("Error ending connection:")).append(e2.getMessage()).toString()); } Display.getDefault().asyncExec(new Runnable() { public void run() { MessageDialog.openError(viewer.getControl().getShell(), "Error", (new StringBuilder()).append(errorMessageFinal).toString()); } }); } } }
From source file:org.apache.jsp.sources_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(";"); return value.substring(equalsLoc + 1, semicolonLoc); }/*from w ww . jav a 2 s . c o m*/ } } } return null; }
From source file:org.talend.librariesmanager.utils.RemoteModulesHelper.java
public String getLicenseContentByUrl(String licenseUrl) { if (licenseUrl != null && licenseUrl.length() > 0) { try {//from ww w . ja v a2 s .c o m URL url = new URL(licenseUrl); URLConnection urlConnection = url.openConnection(); Map<String, List<String>> headerFields = urlConnection.getHeaderFields(); if (headerFields != null) { List<String> contentType = headerFields.get("Content-Type"); //$NON-NLS-1$ if (contentType != null) { if (contentType.contains("text/plain")) { //$NON-NLS-1$ // Get the plain text from connection. InputStream inputStream = urlConnection.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(500); byte[] b = new byte[1024]; int len; while ((len = inputStream.read(b)) != -1) { baos.write(b, 0, len); } inputStream.close(); return baos.toString(); // } else if (contentType.contains("text/html")) { // return url too. } } } return licenseUrl; // else, just return the URL. } catch (MalformedURLException e) { // } catch (IOException e) { // } } return null; }
From source file:org.appcelerator.titanium.util.TiResponseCache.java
@Override public CacheRequest put(URI uri, URLConnection conn) throws IOException { if (cacheDir == null) return null; // Make sure the cacheDir exists, in case user clears cache while app is running if (!cacheDir.exists()) { cacheDir.mkdirs();// www. j a v a 2s .co m } // Gingerbread 2.3 bug: getHeaderField tries re-opening the InputStream // getHeaderFields() just checks the response itself Map<String, List<String>> headers = makeLowerCaseHeaders(conn.getHeaderFields()); String cacheControl = getHeader(headers, "cache-control"); if (cacheControl != null && cacheControl.matches("^.*(no-cache|no-store|must-revalidate).*")) { return null; // See RFC-2616 } boolean skipTransferEncodingHeader = false; String tEncoding = getHeader(headers, "transfer-encoding"); if (tEncoding != null && tEncoding.toLowerCase().equals("chunked")) { skipTransferEncodingHeader = true; // don't put "chunked" transfer-encoding into our header file, else the http connection object that gets our header information will think the data starts with a chunk length specification } // Form the headers and generate the content length String newl = System.getProperty("line.separator"); long contentLength = getHeaderInt(headers, "content-length", 0); StringBuilder sb = new StringBuilder(); for (String hdr : headers.keySet()) { if (!skipTransferEncodingHeader || !hdr.equals("transfer-encoding")) { for (String val : headers.get(hdr)) { sb.append(hdr); sb.append("="); sb.append(val); sb.append(newl); } } } if (contentLength + sb.length() > maxCacheSize) { return null; } // Work around an android bug which gives us the wrong URI try { uri = conn.getURL().toURI(); } catch (URISyntaxException e) { } // Get our key, which is a hash of the URI String hash = DigestUtils.shaHex(uri.toString()); // Make our cache files File hFile = new File(cacheDir, hash + HEADER_SUFFIX); File bFile = new File(cacheDir, hash + BODY_SUFFIX); // Write headers synchronously FileWriter hWriter = new FileWriter(hFile); try { hWriter.write(sb.toString()); } finally { hWriter.close(); } synchronized (this) { // Don't add it to the cache if its already being written if (!bFile.createNewFile()) { return null; } return new TiCacheRequest(uri, bFile, hFile, contentLength); } }
From source file:stringreplacer.rewriting.RewriterServlet.java
/** * This method in the entry-point for forwarding all HTTP requests made. *///w ww. j a va 2 s. com protected void doRequest(HttpServletRequest request, HttpServletResponse response) throws IOException { /* We code the origin server name as the first directory in the URL * being requested. This would normally be setup in an Apache * ProxyPass setting. Here we slice out the origin server name from * the URL. */ final String origin; { String pathInfo = request.getPathInfo(); String[] pathSplit = StringUtils.split(pathInfo, "/"); if (pathSplit.length == 0) { throw new IOException("No origin servername specified on url"); } else { origin = pathSplit[0]; } } /* Since the requesting url that was forwarded is placed after the origin * server name as a directory, we need to remove the origin server name * and get the full URL with query parameters being requested. */ final String path; { String uri = request.getRequestURI(); String query = isNotEmpty(request.getQueryString()) ? "?" + request.getQueryString() : ""; int forwardUriPos = uri.indexOf(origin) + origin.length(); path = uri.substring(forwardUriPos) + query; } final URLConnection connection; try { connection = openUrlConnection(origin, path, request); log("Opening: " + connection); } catch (FileNotFoundException fnfe) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } /* We now search the content type of all forwarded requests for content * types that start with our matching strings because it will be only * these content types that we will act upon to rewrite string data. */ boolean matching = false; { final String originContentType = connection.getContentType().trim().toLowerCase(); for (String contentType : targetContentTypes) { matching = originContentType.startsWith(contentType); if (matching) { break; } } } /* We need to pass all headers that were sent from the origin server * to the client, otherwise the client will get a bunch of garbage * like raw gzipped output. */ { for (String key : connection.getHeaderFields().keySet()) { String value = connection.getHeaderField(key); /* We have received a HTTP relocation request. We will want to * rewrite this url as well. */ if (key != null && key.trim().equalsIgnoreCase("Location")) { log("Redirect: " + value + " => "); value = processStringWithRewriters(value); log(value); } response.setHeader(key, value); } response.setContentType(connection.getContentType()); } /* Use memory to buffer origin request stream otherwise we might experience * some hiccups in performance. */ InputStream in = null; try { in = new BufferedInputStream(connection.getInputStream()); // Rewrite the input stream if (matching) { in = attachNestedStreams(in); copyFromOrigin(in, response); // Do nothing and just copy it } else { copyFromOrigin(in, response); } } finally { IOUtils.closeQuietly(in); } }