List of usage examples for java.net HttpURLConnection getHeaderField
public String getHeaderField(int n)
From source file:MainClass.java
public static void main(String[] args) throws Exception { URL u = new URL("http://www.java2s.com"); HttpURLConnection uc = (HttpURLConnection) u.openConnection(); int code = uc.getResponseCode(); String response = uc.getResponseMessage(); System.out.println("HTTP/1.x " + code + " " + response); for (int j = 1;; j++) { String header = uc.getHeaderField(j); String key = uc.getHeaderFieldKey(j); if (header == null || key == null) break; System.out.println(uc.getHeaderFieldKey(j) + ": " + header); }//from w ww. j a v a2 s. com InputStream in = new BufferedInputStream(uc.getInputStream()); Reader r = new InputStreamReader(in); int c; while ((c = r.read()) != -1) { System.out.print((char) c); } }
From source file:org.oclc.oai.harvester2.verb.HarvesterVerb.java
public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException { String requestURL = "https://databank.ora.ox.ac.uk/oaipmh?verb=ListRecords&resumptionToken=20121206_TKMGW4A_SWT3NHV"; //String requestURL = "http://bd2.inesc-id.pt:8080/repox2Eudml/OAIHandler?verb=ListRecords&resumptionToken=1354116062009:ELibM_external:eudml-article2:33753:37054::"; //String requestURL = "http://bd2.inesc-id.pt:8080/repox2Eudml/OAIHandler?verb=GetRecord&identifier=urn:eudml.eu:ELibM_external:05152756&metadataPrefix=eudml-article2"; //String requestURL = "C:/Users/Gilberto Pedrosa/Desktop/OAIHandler.xml"; //FileInputStream fis = new FileInputStream(requestURL); //InputStream in = fis; logger.debug("requestURL=" + requestURL); DocumentBuilderFactory factory; factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);//from w w w . j ava 2 s .co m Thread t = Thread.currentThread(); DocumentBuilder builder = factory.newDocumentBuilder(); HashMap builderMap = new HashMap(); builderMap.put(t, builder); InputStream in; URL url = new URL(requestURL); HttpURLConnection con; int responseCode; do { con = (HttpURLConnection) url.openConnection(); con.setConnectTimeout(30000); con.setReadTimeout(600000); if (con.getAllowUserInteraction()) { con.setRequestProperty("User-Agent", "OAIHarvester/2.0"); con.setRequestProperty("Accept-Encoding", "compress, gzip, identify"); } try { responseCode = con.getResponseCode(); logger.debug("responseCode=" + responseCode); } catch (FileNotFoundException e) { // assume it's a 503 response logger.error(requestURL, e); responseCode = HttpURLConnection.HTTP_UNAVAILABLE; } if (responseCode == HttpURLConnection.HTTP_UNAVAILABLE) { long retrySeconds = con.getHeaderFieldInt("Retry-After", -1); if (retrySeconds == -1) { long now = (new Date()).getTime(); long retryDate = con.getHeaderFieldDate("Retry-After", now); retrySeconds = retryDate - now; } if (retrySeconds == 0) { // Apparently, it's a bad URL throw new FileNotFoundException("Bad URL?"); } logger.warn("Server response: Retry-After=" + retrySeconds); if (retrySeconds > 0) { try { Thread.sleep(retrySeconds * 1000); } catch (InterruptedException ex) { ex.printStackTrace(); } } } } while (responseCode == HttpURLConnection.HTTP_UNAVAILABLE); String contentEncoding = con.getHeaderField("Content-Encoding"); logger.debug("contentEncoding=" + contentEncoding); if ("compress".equals(contentEncoding)) { ZipInputStream zis = new ZipInputStream(con.getInputStream()); zis.getNextEntry(); in = zis; } else if ("gzip".equals(contentEncoding)) { in = new GZIPInputStream(con.getInputStream()); } else if ("deflate".equals(contentEncoding)) { in = new InflaterInputStream(con.getInputStream()); } else { in = con.getInputStream(); } byte[] inputBytes = IOUtils.toByteArray(in); InputSource data = new InputSource(new ByteArrayInputStream(inputBytes)); String xmlString = new String(inputBytes, "UTF-8"); xmlString = XmlUtil.removeInvalidXMLCharacters(xmlString); builder.parse(data); System.out.println("data = " + data); }
From source file:org.oclc.oai.harvester.verb.HarvesterVerb.java
/** * @param args/*from w w w . ja va 2 s . co m*/ * @throws IOException * @throws ParserConfigurationException * @throws SAXException */ public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException { String requestURL = "https://databank.ora.ox.ac.uk/oaipmh?verb=ListRecords&resumptionToken=20121206_TKMGW4A_SWT3NHV"; //String requestURL = "http://bd2.inesc-id.pt:8080/repox2Eudml/OAIHandler?verb=ListRecords&resumptionToken=1354116062009:ELibM_external:eudml-article2:33753:37054::"; //String requestURL = "http://bd2.inesc-id.pt:8080/repox2Eudml/OAIHandler?verb=GetRecord&identifier=urn:eudml.eu:ELibM_external:05152756&metadataPrefix=eudml-article2"; //String requestURL = "C:/Users/Gilberto Pedrosa/Desktop/OAIHandler.xml"; //FileInputStream fis = new FileInputStream(requestURL); //InputStream in = fis; logger.debug("requestURL=" + requestURL); DocumentBuilderFactory factory; factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); Thread t = Thread.currentThread(); DocumentBuilder builder = factory.newDocumentBuilder(); HashMap<Thread, DocumentBuilder> builderMap = new HashMap<Thread, DocumentBuilder>(); builderMap.put(t, builder); InputStream in; URL url = new URL(requestURL); HttpURLConnection con; int responseCode; do { con = (HttpURLConnection) url.openConnection(); con.setConnectTimeout(30000); con.setReadTimeout(600000); if (con.getAllowUserInteraction()) { con.setRequestProperty("User-Agent", "OAIHarvester/2.0"); con.setRequestProperty("Accept-Encoding", "compress, gzip, identify"); } try { responseCode = con.getResponseCode(); logger.debug("responseCode=" + responseCode); } catch (FileNotFoundException e) { // assume it's a 503 response logger.error(requestURL, e); responseCode = HttpURLConnection.HTTP_UNAVAILABLE; } if (responseCode == HttpURLConnection.HTTP_UNAVAILABLE) { long retrySeconds = con.getHeaderFieldInt("Retry-After", -1); if (retrySeconds == -1) { long now = (new Date()).getTime(); long retryDate = con.getHeaderFieldDate("Retry-After", now); retrySeconds = retryDate - now; } if (retrySeconds == 0) { // Apparently, it's a bad URL throw new FileNotFoundException("Bad URL?"); } logger.warn("Server response: Retry-After=" + retrySeconds); if (retrySeconds > 0) { try { Thread.sleep(retrySeconds * 1000); } catch (InterruptedException ex) { ex.printStackTrace(); } } } } while (responseCode == HttpURLConnection.HTTP_UNAVAILABLE); String contentEncoding = con.getHeaderField("Content-Encoding"); logger.debug("contentEncoding=" + contentEncoding); if ("compress".equals(contentEncoding)) { ZipInputStream zis = new ZipInputStream(con.getInputStream()); zis.getNextEntry(); in = zis; } else if ("gzip".equals(contentEncoding)) { in = new GZIPInputStream(con.getInputStream()); } else if ("deflate".equals(contentEncoding)) { in = new InflaterInputStream(con.getInputStream()); } else { in = con.getInputStream(); } byte[] inputBytes = IOUtils.toByteArray(in); InputSource data = new InputSource(new ByteArrayInputStream(inputBytes)); String xmlString = new String(inputBytes, "UTF-8"); xmlString = XmlUtil.removeInvalidXMLCharacters(xmlString); builder.parse(data); System.out.println("data = " + data); }
From source file:Main.java
private static String getFileName(HttpURLConnection conn) { String fileName = conn.getHeaderField("Content-Disposition"); Matcher matcher = REGEX_FILE_NAME.matcher(fileName); if (matcher.find()) { return matcher.group(1); } else {//from w w w . j ava 2 s . com return null; } }
From source file:Main.java
public static boolean isAuthorizationRequired(HttpURLConnection urlConnection) throws IOException { return isAuthorizationRequired(urlConnection.getResponseCode(), urlConnection.getHeaderField(WWW_AUTHENTICATE_HEADER)); }
From source file:Main.java
protected static String getResponseAsString(HttpURLConnection conn, boolean responseError) throws IOException { String charset = getResponseCharset(conn.getContentType()); String header = conn.getHeaderField("Content-Encoding"); boolean isGzip = false; if (header != null && header.toLowerCase().contains("gzip")) { isGzip = true;//from w w w. j a v a 2s. co m } InputStream es = conn.getErrorStream(); if (es == null) { InputStream input = conn.getInputStream(); if (isGzip) { input = new GZIPInputStream(input); } return getStreamAsString(input, charset); } else { if (isGzip) { es = new GZIPInputStream(es); } String msg = getStreamAsString(es, charset); if (TextUtils.isEmpty(msg)) { throw new IOException(conn.getResponseCode() + ":" + conn.getResponseMessage()); } else if (responseError) { return msg; } else { throw new IOException(msg); } } }
From source file:net.sf.taverna.t2.renderers.RendererUtils.java
public static long getSizeInBytes(Path path) throws IOException { if (isValue(path)) return Files.size(path); if (!isReference(path)) throw new IllegalArgumentException("Path is not a value or reference"); URL url = getReference(path).toURL(); switch (url.getProtocol().toLowerCase()) { case "http": case "https": HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("HEAD"); conn.connect();//from w w w .j av a2s. c om String contentLength = conn.getHeaderField("Content-Length"); conn.disconnect(); if (contentLength != null && !contentLength.isEmpty()) return Long.parseLong(contentLength); return -1; case "file": return FileUtils.toFile(url).length(); default: return -1; } }
From source file:org.osmdroid.location.FourSquareProvider.java
public static void browse(final Context context, POI poi) { // get the right url from redirect, could also parse the result from querying venueid... new AsyncTask<POI, Void, String>() { @Override//from w ww . j a v a 2s . c om protected String doInBackground(POI... params) { POI poi = params[0]; if (poi == null) return null; try { URL url = new URL(poi.url); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setInstanceFollowRedirects(false); String redirect = conn.getHeaderField("Location"); if (redirect != null) { Log.d(BonusPackHelper.LOG_TAG, redirect); return redirect; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String result) { if (result == null) return; Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://foursquare.com" + result)); context.startActivity(myIntent); } }.execute(poi); }
From source file:org.apache.hadoop.hdfs.web.ByteRangeInputStream.java
private static long getStreamLength(HttpURLConnection connection, Map<String, List<String>> headers) throws IOException { String cl = connection.getHeaderField(HttpHeaders.CONTENT_LENGTH); if (cl == null) { // Try to get the content length by parsing the content range // because HftpFileSystem does not return the content length // if the content is partial. if (connection.getResponseCode() == HttpStatus.SC_PARTIAL_CONTENT) { cl = connection.getHeaderField(HttpHeaders.CONTENT_RANGE); return getLengthFromRange(cl); } else {/* w ww . j a va2s . c o m*/ throw new IOException(HttpHeaders.CONTENT_LENGTH + " is missing: " + headers); } } return Long.parseLong(cl); }
From source file:com.github.thorqin.webapi.oauth2.OAuthClient.java
public static <T> T obtainResource(String authorityServerUri, String accessToken, Class<T> type) throws IOException, OAuthException, ClassCastException { URL u = new URL(authorityServerUri); HttpURLConnection conn = (HttpURLConnection) u.openConnection(); conn.setRequestProperty("Authorization", "Bearer " + accessToken); if (conn.getResponseCode() == 401) { String wwwAuth = conn.getHeaderField("WWW-Authenticate"); if (wwwAuth == null) { throw new OAuthException("unauthorized_client"); } else {/*from w ww. j a v a 2s .c om*/ throw new OAuthException(getHeadSubParameter(wwwAuth, "error"), getHeadSubParameter(wwwAuth, "error_description"), getHeadSubParameter(wwwAuth, "error_uri")); } } else if (conn.getResponseCode() == 400) { throw new OAuthException("invalid_request"); } else if (conn.getResponseCode() > 401 && conn.getResponseCode() < 500) { throw new OAuthException("access_denied"); } else if (conn.getResponseCode() >= 500) { throw new OAuthException("server_error"); } else if (conn.getResponseCode() != 200) throw new OAuthException("unsupported_response_type"); try (InputStream is = conn.getInputStream(); InputStreamReader reader = new InputStreamReader(is)) { T resource = Serializer.fromJson(reader, type); return resource; } }