List of usage examples for java.net HttpURLConnection getContentType
public String getContentType()
From source file:Main.java
public static void main(String args[]) throws Exception { URL url = new URL("http://www.google.com"); HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); System.out.println("Content-Type: " + httpCon.getContentType()); }
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 ww w . j a v a 2 s . c o 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:com.thomaskuenneth.openweathermapweather.WeatherUtils.java
public static String getFromServer(String url) throws MalformedURLException, IOException { StringBuilder sb = new StringBuilder(); URL _url = new URL(url); HttpURLConnection httpURLConnection = (HttpURLConnection) _url.openConnection(); String contentType = httpURLConnection.getContentType(); String charSet = "ISO-8859-1"; if (contentType != null) { Matcher m = PATTERN_CHARSET.matcher(contentType); if (m.matches()) { charSet = m.group(1);// w ww . ja va2s .c om } } final int responseCode = httpURLConnection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { InputStreamReader inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream(), charSet); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String line; while ((line = bufferedReader.readLine()) != null) { sb.append(line); } try { bufferedReader.close(); } catch (IOException ex) { LOGGER.log(Level.SEVERE, "getFromServer()", ex); } } httpURLConnection.disconnect(); return sb.toString(); }
From source file:com.risevision.ui.server.utils.MakeRequestServlet.java
private static String getCharsetType(HttpURLConnection connection) { String contentType = connection.getContentType(); String[] values = contentType.split(";"); //The values.length must be equal to 2... String charset = ""; for (String value : values) { value = value.trim();/*from w ww . j av a 2 s. co m*/ if (value.toLowerCase().startsWith("charset=")) { charset = value.substring("charset=".length()).toUpperCase(); // Some servers include quotes around the charset: // Content-Type: text/html; charset="UTF-8" if (charset.length() >= 2 && charset.startsWith("\"") && charset.endsWith("\"")) { charset = charset.substring(1, charset.length() - 1); } } } return charset; }
From source file:com.camel.trainreserve.JDKHttpsClient.java
protected static String getResponseAsString(HttpURLConnection conn) throws IOException { String charset = getResponseCharset(conn.getContentType()); InputStream es = conn.getErrorStream(); if (es == null) { return getStreamAsString(conn.getInputStream(), charset); } else {//from w w w. j ava 2 s .co m String msg = getStreamAsString(es, charset); if (StringUtils.isEmpty(msg)) { throw new IOException(conn.getResponseCode() + ":" + conn.getResponseMessage()); } else { throw new IOException(msg); } } }
From source file:Main.java
public static Bitmap getImageFromURL(URL url) { Bitmap bitmap = null;//from w w w.j av a 2s . c o m HttpURLConnection connection = null; try { connection = (HttpURLConnection) url.openConnection(); if (connection.getResponseCode() != 200) { return null; } if (!CONTENT_TYPE_IMAGE.equalsIgnoreCase(connection.getContentType().substring(0, 5))) { return null; } bitmap = BitmapFactory.decodeStream(connection.getInputStream()); } catch (IOException e) { e.printStackTrace(); } if (connection != null) { connection.disconnect(); } return bitmap; }
From source file:com.zack6849.alphabot.api.Utils.java
public static String getTitle(String link) { String response = ""; try {/*from www . jav a 2 s. c om*/ HttpURLConnection conn = (HttpURLConnection) new URL(link).openConnection(); conn.addRequestProperty("User-Agent", USER_AGENT); String type = conn.getContentType(); int length = conn.getContentLength() / 1024; response = String.format("HTTP %s: %s", conn.getResponseCode(), conn.getResponseMessage()); String info; if (type.contains("text") || type.contains("application")) { Document doc = Jsoup.connect(link).userAgent(USER_AGENT).followRedirects(true).get(); String title = doc.title() == null || doc.title().isEmpty() ? "No title found!" : doc.title(); info = String.format("%s - (Content Type: %s Size: %skb)", title, type, length); return info; } info = String.format("Content Type: %s Size: %skb", type, length); return info; } catch (IOException ex) { if (ex.getMessage().contains("UnknownHostException")) { return Colors.RED + "Unknown hostname!"; } return response.isEmpty() ? Colors.RED + "An error occured" : response; } }
From source file:org.rapidcontext.app.plugin.http.HttpPostProcedure.java
/** * Attempts to guess the HTTP response character set based on the content * type header. Defaults to UTF-8 if no proper character set was specified. * * @param con the HTTP connection * * @return the HTTP response character set *//*w w w . ja va 2 s . c om*/ private static String guessResponseCharset(HttpURLConnection con) { String contentType = con.getContentType().replace(" ", ""); for (String param : contentType.split(";")) { if (param.startsWith("charset=")) { return param.split("=", 2)[1]; } } return "UTF-8"; }
From source file:com.gson.util.HttpKit.java
/** * ?/* w ww.j a v a 2 s. c om*/ * @param url * @return * @throws IOException */ public static Attachment download(String url) throws IOException { Attachment att = new Attachment(); HttpURLConnection conn = initHttp(url, _GET, null); if (conn.getContentType().equalsIgnoreCase("text/plain")) { // BufferedReader???URL? InputStream in = conn.getInputStream(); BufferedReader read = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET)); String valueString = null; StringBuffer bufferRes = new StringBuffer(); while ((valueString = read.readLine()) != null) { bufferRes.append(valueString); } in.close(); att.setError(bufferRes.toString()); } else { BufferedInputStream bis = new BufferedInputStream(conn.getInputStream()); String ds = conn.getHeaderField("Content-disposition"); String fullName = ds.substring(ds.indexOf("filename=\"") + 10, ds.length() - 1); String relName = fullName.substring(0, fullName.lastIndexOf(".")); String suffix = fullName.substring(relName.length() + 1); att.setFullName(fullName); att.setFileName(relName); att.setSuffix(suffix); att.setContentLength(conn.getHeaderField("Content-Length")); att.setContentType(conn.getHeaderField("Content-Type")); att.setFileStream(bis); } return att; }
From source file:Main.java
static void getHTTPXml(URL url) throws Exception { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("ACCEPT", "application/xml"); InputStream xml = conn.getInputStream(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); org.w3c.dom.Document document = builder.parse(xml); System.out.println(document); String doctype = conn.getContentType(); System.out.println(doctype);/*from w ww. j av a 2 s. c o m*/ XPathFactory pathFactory = XPathFactory.newInstance(); XPath path = pathFactory.newXPath(); XPathExpression expression; expression = path.compile("/result/checkid"); NodeList nodeList = (NodeList) expression.evaluate(document, XPathConstants.NODESET); String checkids[] = getNodeValue(nodeList); for (String checkid : checkids) { System.out.print(checkid + ", "); } conn.disconnect(); }