List of usage examples for java.net HttpURLConnection getHeaderField
public String getHeaderField(int n)
From source file:br.bireme.tb.URLS.java
/** * Given an url, loads its content (GET - method) * @param url url to be loaded/* w w w. ja v a2s . co m*/ * @return an array with the real location of the page (in case of redirect) * and its content. * @throws IOException */ public static String[] loadPageGet(final URL url) throws IOException { if (url == null) { throw new NullPointerException("url"); } System.out.print("loading page (GET) : [" + url + "]"); HttpURLConnection.setFollowRedirects(false); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Accept-Charset", DEFAULT_ENCODING); connection.setRequestProperty("User-Agent", "curl/7.29.0"); connection.setRequestProperty("Accept", "*/*"); connection.connect(); int respCode = connection.getResponseCode(); final StringBuilder builder = new StringBuilder(); String location = url.toString(); while ((respCode >= 300) && (respCode <= 399)) { location = connection.getHeaderField("Location"); connection = (HttpURLConnection) new URL(location).openConnection(); respCode = connection.getResponseCode(); } final boolean respCodeOk = (respCode == 200); final BufferedReader reader; boolean skipLine = false; if (respCodeOk) { reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), DEFAULT_ENCODING)); } else { reader = new BufferedReader(new InputStreamReader(connection.getErrorStream(), DEFAULT_ENCODING)); } while (true) { String line = reader.readLine(); if (line == null) { break; } final String line2 = line.trim(); if (line2.startsWith("<!--")) { if (line2.endsWith("-->")) { continue; } skipLine = true; } else if (line2.endsWith("-->")) { skipLine = false; line = ""; } if (!skipLine) { builder.append(line); builder.append("\n"); } } reader.close(); connection.disconnect(); if (!respCodeOk) { throw new IOException("url=[" + url + "]\ncode=" + respCode + "\n" + builder.toString()); } //System.out.print("+"); System.out.println(" - OK"); return new String[] { location, builder.toString() }; }
From source file:com.romeikat.datamessie.core.base.service.download.AbstractDownloader.java
protected String getResponseUrl(final URLConnection urlConnection) throws IOException { if (!(urlConnection instanceof HttpURLConnection)) { return urlConnection.getURL().toExternalForm(); }//from w ww. j a va 2 s . c o m final HttpURLConnection httpUrlConnection = (HttpURLConnection) urlConnection; httpUrlConnection.setInstanceFollowRedirects(false); final String responseUrl = httpUrlConnection.getHeaderField("Location"); return responseUrl; }
From source file:com.bnrc.util.AbFileUtil.java
/** * ????xx.??./*from ww w . j av a 2 s .c o m*/ * @param connection ? * @return ?? */ public static String getRealFileName(HttpURLConnection connection) { String name = null; try { if (connection == null) { return name; } if (connection.getResponseCode() == 200) { for (int i = 0;; i++) { String mime = connection.getHeaderField(i); if (mime == null) { break; } // "Content-Disposition","attachment; filename=1.txt" // Content-Length if ("content-disposition".equals(connection.getHeaderFieldKey(i).toLowerCase())) { Matcher m = Pattern.compile(".*filename=(.*)").matcher(mime.toLowerCase()); if (m.find()) { return m.group(1).replace("\"", ""); } } } } } catch (Exception e) { e.printStackTrace(); AbLogUtil.e(AbFileUtil.class, "??"); } return name; }
From source file:net.sf.taverna.t2.reference.impl.external.http.HttpReference.java
@Override public Long getApproximateSizeInBytes() { long now = currentTimeMillis(); if (cachedLength != null && cacheTime != null && cacheTime.getTime() + CACHE_TIMEOUT > now) return cachedLength; try {/*from www. java 2 s. c o m*/ HttpURLConnection c = (HttpURLConnection) httpUrl.openConnection(); c.setRequestMethod("HEAD"); c.connect(); String lenString = c.getHeaderField("Content-Length"); if (lenString != null && !lenString.isEmpty()) { cachedLength = new Long(lenString); cacheTime = new Date(now); return cachedLength; } // there is no Content-Length field so we cannot know the size } catch (Exception e) { // something went wrong, but we don't care what } cachedLength = null; cacheTime = null; return new Long(-1); }
From source file:com.dv.Utils.DvFileUtil.java
/** * ???xx.??./*from w ww. j a va2s. c o m*/ * @param connection * @return ?? */ public static String getRealFileName(HttpURLConnection connection) { String name = null; try { if (connection == null) { return name; } if (connection.getResponseCode() == 200) { for (int i = 0;; i++) { String mime = connection.getHeaderField(i); if (mime == null) { break; } // "Content-Disposition","attachment; filename=1.txt" // Content-Length if ("content-disposition".equals(connection.getHeaderFieldKey(i).toLowerCase())) { Matcher m = Pattern.compile(".*filename=(.*)").matcher(mime.toLowerCase()); if (m.find()) { return m.group(1).replace("\"", ""); } } } } } catch (Exception e) { e.printStackTrace(); DvLogUtil.e(DvFileUtil.class, "???"); } return name; }
From source file:org.apache.olingo.fit.tecsvc.http.BasicStreamITCase.java
@Test public void streamESStreamJson() throws Exception { URL url = new URL(SERVICE_URI + "ESStream?$format=json"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(HttpMethod.GET.name()); connection.connect();//from w w w .jav a 2 s. c o m assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode()); assertEquals(ContentType.JSON, ContentType.create(connection.getHeaderField(HttpHeader.CONTENT_TYPE))); final String content = IOUtils.toString(connection.getInputStream()); assertTrue(content.contains("Streamed-Employee1@company.example\"," + "\"Streamed-Employee2@company.example\"," + "\"Streamed-Employee3@company.example\"")); assertTrue(content.contains("\"PropertyString\":\"TEST 1->streamed\"")); assertTrue(content.contains("\"PropertyString\":\"TEST 2->streamed\"")); }
From source file:h2weibo.utils.filters.FlickrImageFilter.java
private String extraceFromShortURL(String input) { Pattern p = Pattern.compile("flic.kr/p/(\\w+)/?.*"); Matcher m = p.matcher(input); if (m.find()) { input = "http://www.flickr.com/photo.gne?short=" + m.group(1); String imageUrl = null;/*from www . j a v a 2 s . co m*/ try { URL url = new URL(input); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setInstanceFollowRedirects(false); conn.connect(); int code = conn.getResponseCode(); if (code == 302) { String loc = conn.getHeaderField("location"); imageUrl = extraceFromFullURL("http://www.flickr.com" + loc); } conn.disconnect(); } catch (Exception e) { log.error("Not able to handle flickr's short url/", e); } return imageUrl; } else { return null; } }
From source file:cn.org.eshow.framwork.util.AbFileUtil.java
/** * ???xx.??.//from www. j a v a2 s. co m * @param connection * @return ?? */ public static String getRealFileName(HttpURLConnection connection) { String name = null; try { if (connection == null) { return name; } if (connection.getResponseCode() == 200) { for (int i = 0;; i++) { String mime = connection.getHeaderField(i); if (mime == null) { break; } // "Content-Disposition","attachment; filename=1.txt" // Content-Length if ("content-disposition".equals(connection.getHeaderFieldKey(i).toLowerCase())) { Matcher m = Pattern.compile(".*filename=(.*)").matcher(mime.toLowerCase()); if (m.find()) { return m.group(1).replace("\"", ""); } } } } } catch (Exception e) { e.printStackTrace(); AbLogUtil.e(AbFileUtil.class, "???"); } return name; }
From source file:org.omegat.gui.glossary.taas.TaaSClient.java
/** * Check content type of response.//from w w w . j a v a2 s . c om */ void checkXMLContentType(HttpURLConnection conn) throws FormatError { String contentType = conn.getHeaderField("Content-Type"); if (contentType == null) { throw new FormatError("Empty Content-Type header"); } String ct = contentType.replace(" ", "").toLowerCase(); if (!"text/xml".equals(ct) && !"application/xml".equals(ct)) { throw new FormatError("Wrong Content-Type header: " + contentType); } }