本文整理了Java中java.net.HttpURLConnection.getContentType()
方法的一些代码示例,展示了HttpURLConnection.getContentType()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpURLConnection.getContentType()
方法的具体详情如下:
包路径:java.net.HttpURLConnection
类名称:HttpURLConnection
方法名:getContentType
暂无
代码示例来源:origin: square/okhttp
@Override public String getContentType() {
return delegate.getContentType();
}
代码示例来源:origin: prestodb/presto
@Override public String getContentType() {
return delegate.getContentType();
}
代码示例来源:origin: alibaba/nacos
private static String getCharset(HttpURLConnection conn) {
String contentType = conn.getContentType();
if (StringUtils.isEmpty(contentType)) {
return "UTF-8";
}
String[] values = contentType.split(";");
if (values.length == 0) {
return "UTF-8";
}
String charset = "UTF-8";
for (String value : values) {
value = value.trim();
if (value.toLowerCase().startsWith("charset=")) {
charset = value.substring("charset=".length());
}
}
return charset;
}
代码示例来源:origin: looly/hutool
/**
* 从Http连接的头信息中获得字符集<br>
* 从ContentType中获取
*
* @param conn HTTP连接对象
* @return 字符集
*/
public static String getCharset(HttpURLConnection conn) {
if (conn == null) {
return null;
}
return ReUtil.get(CHARSET_PATTERN, conn.getContentType(), 1);
}
代码示例来源:origin: looly/hutool
/**
* 从Http连接的头信息中获得字符集<br>
* 从ContentType中获取
*
* @param conn HTTP连接对象
* @return 字符集
*/
public static String getCharset(HttpURLConnection conn) {
if (conn == null) {
return null;
}
return ReUtil.get(CHARSET_PATTERN, conn.getContentType(), 1);
}
代码示例来源:origin: alibaba/nacos
private static String getCharset(HttpURLConnection conn) {
String contentType = conn.getContentType();
if (StringUtils.isEmpty(contentType)) {
return "UTF-8";
}
String[] values = contentType.split(";");
if (values.length == 0) {
return "UTF-8";
}
String charset = "UTF-8";
for (String value : values) {
value = value.trim();
if (value.toLowerCase().startsWith("charset=")) {
charset = value.substring("charset=".length());
}
}
return charset;
}
代码示例来源:origin: ltsopensource/light-task-scheduler
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 {
return getStreamAsString(es, charset);
}
}
代码示例来源:origin: ltsopensource/light-task-scheduler
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 {
return getStreamAsString(es, charset);
}
}
代码示例来源:origin: airbnb/lottie-android
FileExtension extension;
LottieResult<LottieComposition> result;
switch (connection.getContentType()) {
case "application/zip":
L.debug("Handling zip response.");
代码示例来源:origin: CarGuo/GSYVideoPlayer
@Override
public void open(long offset) throws ProxyCacheException {
try {
connection = openConnection(offset, -1);
String mime = connection.getContentType();
inputStream = new BufferedInputStream(connection.getInputStream(), DEFAULT_BUFFER_SIZE);
long length = readSourceAvailableBytes(connection, offset, connection.getResponseCode());
this.sourceInfo = new SourceInfo(sourceInfo.url, length, mime);
this.sourceInfoStorage.put(sourceInfo.url, sourceInfo);
} catch (IOException e) {
throw new ProxyCacheException("Error opening connection for " + sourceInfo.url + " with offset " + offset, e);
}
}
代码示例来源:origin: wildfly/wildfly
private HttpURLConnection sendGetRequest(String resourceUrl, int expectedResponseCode, String expectedContentType) throws AcmeException {
try {
final URL directoryUrl = new URL(resourceUrl);
HttpURLConnection connection = (HttpURLConnection) directoryUrl.openConnection();
connection.setRequestMethod(GET);
connection.setRequestProperty(ACCEPT_LANGUAGE, Locale.getDefault().toLanguageTag());
connection.setRequestProperty(USER_AGENT, USER_AGENT_STRING);
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode != expectedResponseCode) {
handleAcmeErrorResponse(connection, responseCode);
}
String contentType = connection.getContentType();
if (! checkContentType(connection, expectedContentType)) {
throw acme.unexpectedContentTypeFromAcmeServer(contentType);
}
return connection;
} catch (Exception e) {
if (e instanceof AcmeException) {
throw (AcmeException) e;
} else {
throw new AcmeException(e);
}
}
}
代码示例来源:origin: mcxiaoke/android-volley
/**
* Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
* @param connection
* @return an HttpEntity populated with data from <code>connection</code>.
*/
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
BasicHttpEntity entity = new BasicHttpEntity();
InputStream inputStream;
try {
inputStream = connection.getInputStream();
} catch (IOException ioe) {
inputStream = connection.getErrorStream();
}
entity.setContent(inputStream);
entity.setContentLength(connection.getContentLength());
entity.setContentEncoding(connection.getContentEncoding());
entity.setContentType(connection.getContentType());
return entity;
}
代码示例来源:origin: org.jsoup/jsoup
private void setupFromConnection(HttpURLConnection conn, Connection.Response previousResponse) throws IOException {
method = Method.valueOf(conn.getRequestMethod());
url = conn.getURL();
statusCode = conn.getResponseCode();
statusMessage = conn.getResponseMessage();
contentType = conn.getContentType();
Map<String, List<String>> resHeaders = createHeaderMap(conn);
processResponseHeaders(resHeaders);
// if from a redirect, map previous response cookies into this response
if (previousResponse != null) {
for (Map.Entry<String, String> prevCookie : previousResponse.cookies().entrySet()) {
if (!hasCookie(prevCookie.getKey()))
cookie(prevCookie.getKey(), prevCookie.getValue());
}
}
}
代码示例来源:origin: chentao0707/SimplifyReader
/**
* Initializes an {@link org.apache.http.HttpEntity} from the given {@link java.net.HttpURLConnection}.
* @param connection
* @return an HttpEntity populated with data from <code>connection</code>.
*/
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
BasicHttpEntity entity = new BasicHttpEntity();
InputStream inputStream;
try {
inputStream = connection.getInputStream();
} catch (IOException ioe) {
inputStream = connection.getErrorStream();
}
entity.setContent(inputStream);
entity.setContentLength(connection.getContentLength());
entity.setContentEncoding(connection.getContentEncoding());
entity.setContentType(connection.getContentType());
return entity;
}
代码示例来源:origin: oracle/helidon
@Override
protected ConfigParser.Content content() throws ConfigException {
try {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(GET_METHOD);
final String mediaType = mediaType(connection.getContentType());
final Optional<Instant> timestamp;
if (connection.getLastModified() != 0) {
timestamp = Optional.of(Instant.ofEpochMilli(connection.getLastModified()));
} else {
timestamp = Optional.of(Instant.now());
LOGGER.fine("Missing GET '" + url + "' response header 'Last-Modified'. Used current time '"
+ timestamp + "' as a content timestamp.");
}
Reader reader = new InputStreamReader(connection.getInputStream(),
ConfigUtils.getContentCharset(connection.getContentEncoding()));
return ConfigParser.Content.create(reader, mediaType, timestamp);
} catch (ConfigException ex) {
throw ex;
} catch (Exception ex) {
throw new ConfigException("Configuration at url '" + url + "' GET is not accessible.", ex);
}
}
代码示例来源:origin: jiangqqlmj/FastDev4Android
/**
* Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
* @param connection
* @return an HttpEntity populated with data from <code>connection</code>.
*/
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
BasicHttpEntity entity = new BasicHttpEntity();
InputStream inputStream;
try {
inputStream = connection.getInputStream();
} catch (IOException ioe) {
inputStream = connection.getErrorStream();
}
entity.setContent(inputStream);
entity.setContentLength(connection.getContentLength());
entity.setContentEncoding(connection.getContentEncoding());
entity.setContentType(connection.getContentType());
return entity;
}
代码示例来源:origin: wildfly/wildfly
private static boolean checkContentType(HttpURLConnection connection, String expectedMediaType) throws AcmeException {
String contentType = connection.getContentType();
if (contentType == null) {
return false;
}
CodePointIterator cpi = CodePointIterator.ofString(contentType);
CodePointIterator di = cpi.delimitedBy(CONTENT_TYPE_DELIMS);
String mediaType = di.drainToString().trim();
skipDelims(di, cpi, CONTENT_TYPE_DELIMS);
while (di.hasNext()) {
String parameter = di.drainToString().trim();
skipDelims(di, cpi, CONTENT_TYPE_DELIMS);
if (parameter.equalsIgnoreCase(CHARSET)) {
String value = di.drainToString().trim();
if (! value.equalsIgnoreCase(UTF_8)) {
return false;
}
}
}
return mediaType.equalsIgnoreCase(expectedMediaType);
}
代码示例来源:origin: CarGuo/GSYVideoPlayer
private void fetchContentInfo() throws ProxyCacheException {
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
try {
urlConnection = openConnection(0, 10000);
long length = getContentLength(urlConnection);
String mime = urlConnection.getContentType();
inputStream = urlConnection.getInputStream();
this.sourceInfo = new SourceInfo(sourceInfo.url, length, mime);
this.sourceInfoStorage.put(sourceInfo.url, sourceInfo);
} catch (IOException e) {
HttpProxyCacheDebuger.printfError("Error fetching info from " + sourceInfo.url, e);
} finally {
ProxyCacheUtils.close(inputStream);
if (urlConnection != null) {
urlConnection.disconnect();
}
}
}
代码示例来源:origin: apache/flink
@Test
public void testResponseHeaders() throws Exception {
// check headers for successful json response
URL taskManagersUrl = new URL("http://localhost:" + getRestPort() + "/taskmanagers");
HttpURLConnection taskManagerConnection = (HttpURLConnection) taskManagersUrl.openConnection();
taskManagerConnection.setConnectTimeout(100000);
taskManagerConnection.connect();
if (taskManagerConnection.getResponseCode() >= 400) {
// error!
InputStream is = taskManagerConnection.getErrorStream();
String errorMessage = IOUtils.toString(is, ConfigConstants.DEFAULT_CHARSET);
throw new RuntimeException(errorMessage);
}
// we don't set the content-encoding header
Assert.assertNull(taskManagerConnection.getContentEncoding());
Assert.assertEquals("application/json; charset=UTF-8", taskManagerConnection.getContentType());
// check headers in case of an error
URL notFoundJobUrl = new URL("http://localhost:" + getRestPort() + "/jobs/dontexist");
HttpURLConnection notFoundJobConnection = (HttpURLConnection) notFoundJobUrl.openConnection();
notFoundJobConnection.setConnectTimeout(100000);
notFoundJobConnection.connect();
if (notFoundJobConnection.getResponseCode() >= 400) {
// we don't set the content-encoding header
Assert.assertNull(notFoundJobConnection.getContentEncoding());
Assert.assertEquals("application/json; charset=UTF-8", notFoundJobConnection.getContentType());
} else {
throw new RuntimeException("Request for non-existing job did not return an error.");
}
}
代码示例来源:origin: hs-web/hsweb-framework
suffix = MIMEType.getSuffix( connection.getContentType() );
内容来源于网络,如有侵权,请联系作者删除!