java.net.HttpURLConnection.getContent()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(6.8k)|赞(0)|评价(0)|浏览(121)

本文整理了Java中java.net.HttpURLConnection.getContent()方法的一些代码示例,展示了HttpURLConnection.getContent()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpURLConnection.getContent()方法的具体详情如下:
包路径:java.net.HttpURLConnection
类名称:HttpURLConnection
方法名:getContent

HttpURLConnection.getContent介绍

[英]Returns the encoding used to transmit the response body over the network. This is null or "identity" if the content was not encoded, or "gzip" if the body was gzip compressed. Most callers will be more interested in the #getContentType(), which may also include the content's character encoding.
[中]返回用于通过网络传输响应正文的编码。如果内容未编码,则为null或“identity”;如果正文是gzip压缩的,则为“gzip”。大多数调用者对#getContentType()更感兴趣,它还可能包括内容的字符编码。

代码示例

代码示例来源:origin: square/okhttp

@Override public Object getContent(Class[] types) throws IOException {
 return delegate.getContent(types);
}

代码示例来源:origin: square/okhttp

@Override public Object getContent() throws IOException {
 return delegate.getContent();
}

代码示例来源:origin: prestodb/presto

@SuppressWarnings("unchecked") // Spec does not generify
@Override public Object getContent(Class[] types) throws IOException {
 return delegate.getContent(types);
}

代码示例来源:origin: prestodb/presto

@Override public Object getContent() throws IOException {
 return delegate.getContent();
}

代码示例来源:origin: checkstyle/checkstyle

private static boolean isUrlReachable(String url) {
  boolean result = true;
  try {
    final URL verifiableUrl = new URL(url);
    final HttpURLConnection urlConnect = (HttpURLConnection) verifiableUrl.openConnection();
    urlConnect.getContent();
  }
  catch (IOException ignored) {
    result = false;
  }
  return result;
}

代码示例来源:origin: checkstyle/checkstyle

private static boolean isUrlReachable(String url) {
  boolean result = true;
  try {
    final URL verifiableUrl = new URL(url);
    final HttpURLConnection urlConnect = (HttpURLConnection) verifiableUrl.openConnection();
    urlConnect.getContent();
  }
  catch (IOException ignored) {
    result = false;
  }
  return result;
}

代码示例来源:origin: Netflix/Priam

public static String getDataFromUrl(String url) {
  try {
    HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
    conn.setConnectTimeout(1000);
    conn.setReadTimeout(1000);
    conn.setRequestMethod("GET");
    if (conn.getResponseCode() != 200) {
      throw new RuntimeException("Unable to get data for URL " + url);
    }
    byte[] b = new byte[2048];
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DataInputStream d = new DataInputStream((FilterInputStream) conn.getContent());
    int c;
    while ((c = d.read(b, 0, b.length)) != -1) bos.write(b, 0, c);
    String return_ = new String(bos.toByteArray(), Charsets.UTF_8);
    logger.info("Calling URL API: {} returns: {}", url, return_);
    conn.disconnect();
    return return_;
  } catch (Exception ex) {
    throw new RuntimeException(ex);
  }
}

代码示例来源:origin: Netflix/Priam

public static String fetchData(String url) {
    DataInputStream responseStream = null;
    try {
      HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
      conn.setConnectTimeout(1000);
      conn.setReadTimeout(10000);
      conn.setRequestMethod("GET");
      if (conn.getResponseCode() != 200)
        throw new RuntimeException("Unable to get data for URL " + url);

      byte[] b = new byte[2048];
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      responseStream = new DataInputStream((FilterInputStream) conn.getContent());
      int c = 0;
      while ((c = responseStream.read(b, 0, b.length)) != -1) bos.write(b, 0, c);
      String return_ = new String(bos.toByteArray(), Charsets.UTF_8);
      logger.info("Calling URL API: {} returns: {}", url, return_);
      conn.disconnect();
      return return_;
    } catch (Exception ex) {
      throw new RuntimeException(ex);
    } finally {
      try {
        if (responseStream != null) responseStream.close();
      } catch (Exception e) {
        logger.warn("Failed to close response stream from priam", e);
      }
    }
  }
}

代码示例来源:origin: eclipse-vertx/vert.x

public static JsonObject getContent() throws IOException {
 URL url = new URL("http://localhost:8080");
 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
 conn.connect();
 InputStreamReader in = new InputStreamReader((InputStream) conn.getContent());
 BufferedReader buff = new BufferedReader(in);
 String line;
 StringBuilder builder = new StringBuilder();
 do {
  line = buff.readLine();
  builder.append(line).append("\n");
 } while (line != null);
 buff.close();
 return new JsonObject(builder.toString());
}

代码示例来源:origin: openmrs/openmrs-core

/**
 * Tests the connection to the specified URL
 *
 * @param urlString the url to test
 * @return true if a connection a established otherwise false
 */
protected static boolean testConnection(String urlString) {
  try {
    HttpURLConnection urlConnect = (HttpURLConnection) new URL(urlString).openConnection();
    //wait for 15sec
    urlConnect.setConnectTimeout(15000);
    urlConnect.setUseCaches(false);
    //trying to retrieve data from the source. If there
    //is no connection, this line will fail
    urlConnect.getContent();
    return true;
  }
  catch (IOException e) {
    if (log.isDebugEnabled()) {
      log.debug("Error generated:", e);
    }
  }
  
  return false;
}

代码示例来源:origin: apache/pdfbox

try (InputStream in = (InputStream) httpConnection.getContent())

代码示例来源:origin: io.vertx/vertx-core

public static JsonObject getContent() throws IOException {
 URL url = new URL("http://localhost:8080");
 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
 conn.connect();
 InputStreamReader in = new InputStreamReader((InputStream) conn.getContent());
 BufferedReader buff = new BufferedReader(in);
 String line;
 StringBuilder builder = new StringBuilder();
 do {
  line = buff.readLine();
  builder.append(line).append("\n");
 } while (line != null);
 buff.close();
 return new JsonObject(builder.toString());
}

代码示例来源:origin: com.squareup.okhttp/okhttp-urlconnection

@SuppressWarnings("unchecked") // Spec does not generify
@Override public Object getContent(Class[] types) throws IOException {
 return delegate.getContent(types);
}

代码示例来源:origin: stevensouza/automon

public int urlWithConnection(String urlString) throws IOException {
  URL url = new URL(urlString);
  HttpURLConnection connection = (HttpURLConnection)url.openConnection();
  connection.setRequestMethod("GET");
  connection.connect();
  connection.getContent();
  return connection.getResponseCode();
}

代码示例来源:origin: jcifs/jcifs

public Object getContent() throws IOException {
  try {
    handshake();
  } catch (IOException ex) { }
  return connection.getContent();
}

代码示例来源:origin: com.jaeksoft/jcifs-krb5-jdk7

public Object getContent() throws IOException {
  try {
    handshake();
  } catch (IOException ex) { }
  return connection.getContent();
}

代码示例来源:origin: jcifs/jcifs

public Object getContent(Class[] classes) throws IOException {
  try {
    handshake();
  } catch (IOException ex) { }
  return connection.getContent(classes);
}

代码示例来源:origin: org.codelibs/jcifs

@Override
public Object getContent ( Class[] classes ) throws IOException {
  handshake();
  return this.connection.getContent(classes);
}

代码示例来源:origin: AgNO3/jcifs-ng

@Override
public Object getContent () throws IOException {
  handshake();
  return this.connection.getContent();
}

代码示例来源:origin: itext/itext7

static InputStream getHttpResponse(URL urlt) throws IOException {
  HttpURLConnection con = (HttpURLConnection)urlt.openConnection();
  if (con.getResponseCode() / 100 != 2) {
    throw new PdfException(PdfException.InvalidHttpResponse1).setMessageParams(con.getResponseCode());
  }
  return (InputStream) con.getContent();
}

相关文章

HttpURLConnection类方法