本文整理了Java中java.net.HttpURLConnection.getInputStream()
方法的一些代码示例,展示了HttpURLConnection.getInputStream()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpURLConnection.getInputStream()
方法的具体详情如下:
包路径:java.net.HttpURLConnection
类名称:HttpURLConnection
方法名:getInputStream
暂无
canonical example by Tabnine
public void postRequest(String urlStr, String jsonBodyStr) throws IOException {
URL url = new URL(urlStr);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Content-Type", "application/json");
try (OutputStream outputStream = httpURLConnection.getOutputStream()) {
outputStream.write(jsonBodyStr.getBytes());
outputStream.flush();
}
if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()))) {
String line;
while ((line = bufferedReader.readLine()) != null) {
// ... do something with line
}
}
} else {
// ... do something with unsuccessful response
}
}
代码示例来源:origin: skylot/jadx
private static <T> T get(String url, Type type) throws IOException {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
if (con.getResponseCode() == 200) {
Reader reader = new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8);
return GSON.fromJson(reader, type);
}
return null;
}
}
代码示例来源:origin: jenkinsci/jenkins
private URL tryToResolveRedirects(URL base, String authorization) {
try {
HttpURLConnection con = (HttpURLConnection) base.openConnection();
if (authorization != null) {
con.addRequestProperty("Authorization", authorization);
}
con.getInputStream().close();
base = con.getURL();
} catch (Exception ex) {
// Do not obscure the problem propagating the exception. If the problem is real it will manifest during the
// actual exchange so will be reported properly there. If it is not real (no permission in UI but sufficient
// for CLI connection using one of its mechanisms), there is no reason to bother user about it.
LOGGER.log(Level.FINE, "Failed to resolve potential redirects", ex);
}
return base;
}
代码示例来源:origin: google/data-transfer-project
private InputStream getImageAsStream(String urlStr) throws IOException {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
return conn.getInputStream();
}
}
代码示例来源:origin: ethereum/ethereumj
private String sendPost(String urlParams) {
try {
HttpURLConnection con = (HttpURLConnection) rpcUrl.openConnection();
//add reuqest header
con.setRequestMethod("POST");
// Send post request
con.setDoOutput(true);
try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {
wr.writeBytes(urlParams);
wr.flush();
}
int responseCode = con.getResponseCode();
if (responseCode != 200) {
throw new RuntimeException("HTTP Response: " + responseCode);
}
final StringBuffer response;
try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
String inputLine;
response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
}
return response.toString();
} catch (IOException e) {
throw new RuntimeException("Error sending POST to " + rpcUrl, e);
}
}
代码示例来源: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: ctripcorp/apollo
InputStreamReader esr = null;
try {
isr = new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8);
CharStreams.toString(isr);
} catch (IOException e) {
esr = new InputStreamReader(errorStream, StandardCharsets.UTF_8);
try {
CharStreams.toString(esr);
代码示例来源:origin: testcontainers/testcontainers-java
private String getResponseBody(HttpURLConnection connection) throws IOException {
BufferedReader reader;
if (200 <= connection.getResponseCode() && connection.getResponseCode() <= 299) {
reader = new BufferedReader(new InputStreamReader((connection.getInputStream())));
} else {
reader = new BufferedReader(new InputStreamReader((connection.getErrorStream())));
}
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
return builder.toString();
}
}
代码示例来源:origin: bumptech/glide
@Before
public void setUp() throws IOException {
MockitoAnnotations.initMocks(this);
URL url = new URL("http://www.google.com");
when(connectionFactory.build(eq(url))).thenReturn(urlConnection);
when(urlConnection.getInputStream()).thenReturn(stream);
when(urlConnection.getResponseCode()).thenReturn(200);
when(glideUrl.toURL()).thenReturn(url);
fetcher = new HttpUrlFetcher(glideUrl, TIMEOUT_MS, connectionFactory);
}
代码示例来源:origin: facebook/stetho
@Override
public String call() throws IOException {
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
int statusCode = connection.getResponseCode();
if (statusCode != 200) {
throw new IOException("Got status code: " + statusCode + " while downloading " +
"schema with url: " + url.toString());
}
InputStream urlStream = connection.getInputStream();
try {
return Util.readAsUTF8(urlStream);
} finally {
urlStream.close();
}
}
}
代码示例来源:origin: Alluxio/alluxio
@Override
public void close() throws IOException {
mOutputStream.close();
InputStream is = null;
try {
// Status 400 and up should be read from error stream.
// Expecting here 201 Create or 202 Accepted.
if (mHttpCon.getResponseCode() >= 400) {
LOG.error("Failed to write data to Swift with error code: " + mHttpCon.getResponseCode());
is = mHttpCon.getErrorStream();
} else {
is = mHttpCon.getInputStream();
}
is.close();
} catch (Exception e) {
LOG.error(e.getMessage());
if (is != null) {
is.close();
}
}
mHttpCon.disconnect();
}
代码示例来源:origin: jmdhappy/xxpay-master
/**
* 以http get方式通信
*
* @param url
* @throws IOException
*/
protected void httpGetMethod(String url) throws IOException {
HttpURLConnection httpConnection =
HttpClientUtil.getHttpURLConnection(url);
this.setHttpRequest(httpConnection);
httpConnection.setRequestMethod("GET");
this.responseCode = httpConnection.getResponseCode();
this.inputStream = httpConnection.getInputStream();
}
代码示例来源:origin: googleapis/google-cloud-java
protected final String sendPostRequest(String request) throws IOException {
URL url = new URL("http", DEFAULT_HOST, this.port, request);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
OutputStream out = con.getOutputStream();
out.write("".getBytes());
out.flush();
InputStream in = con.getInputStream();
String response = CharStreams.toString(new InputStreamReader(con.getInputStream()));
in.close();
return response;
}
代码示例来源:origin: google/data-transfer-project
/**
* Gets an input stream to an image, given its URL.
*/
public InputStream get(String urlStr) throws IOException {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
return conn.getInputStream();
}
}
代码示例来源:origin: oracle/helidon
@Override
protected Data<OverrideData, Instant> loadData() throws ConfigException {
try {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(GET_METHOD);
Instant timestamp;
if (connection.getLastModified() != 0) {
timestamp = Instant.ofEpochMilli(connection.getLastModified());
} else {
timestamp = 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 new Data<>(Optional.of(OverrideData.create(reader)), Optional.of(timestamp));
} catch (ConfigException ex) {
throw ex;
} catch (Exception ex) {
throw new ConfigException("Configuration at url '" + url + "' GET is not accessible.", ex);
}
}
代码示例来源:origin: sohutv/cachecloud
private static String getContent(HttpURLConnection urlConn, String encoding) {
try {
String responseContent = null;
InputStream in = urlConn.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(in, encoding));
String tempLine = rd.readLine();
StringBuffer tempStr = new StringBuffer();
String crlf = System.getProperty("line.separator");
while (tempLine != null) {
tempStr.append(tempLine);
tempStr.append(crlf);
tempLine = rd.readLine();
}
responseContent = tempStr.toString();
rd.close();
in.close();
return responseContent;
} catch (Exception e) {
logger.error(e.getMessage(), e);
return null;
}
}
代码示例来源:origin: google/physical-web
urlConnection = (HttpURLConnection) mUrl.openConnection();
writeToUrlConnection(urlConnection);
responseCode = urlConnection.getResponseCode();
inputStream = new BufferedInputStream(urlConnection.getInputStream());
result = readInputStream(inputStream);
} catch (IOException e) {
代码示例来源:origin: alibaba/nacos
private static HttpResult getResult(HttpURLConnection conn) throws IOException {
int respCode = conn.getResponseCode();
InputStream inputStream;
if (HttpURLConnection.HTTP_OK == respCode) {
inputStream = conn.getInputStream();
} else {
inputStream = conn.getErrorStream();
}
Map<String, String> respHeaders = new HashMap<String, String>(conn.getHeaderFields().size());
for (Map.Entry<String, List<String>> entry : conn.getHeaderFields().entrySet()) {
respHeaders.put(entry.getKey(), entry.getValue().get(0));
}
String gzipEncoding = "gzip";
if (gzipEncoding.equals(respHeaders.get(HttpHeaders.CONTENT_ENCODING))) {
inputStream = new GZIPInputStream(inputStream);
}
HttpResult result = new HttpResult(respCode, IOUtils.toString(inputStream, getCharset(conn)), respHeaders);
inputStream.close();
return result;
}
代码示例来源:origin: apache/ignite
/**
* @param url Url.
* @return Contents.
* @throws IOException If failed.
*/
private String getHttpContents(URL url) throws IOException {
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
int code = conn.getResponseCode();
if (code != 200)
throw null;
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
return rd.lines().collect(Collectors.joining());
}
代码示例来源:origin: google/data-transfer-project
/**
* Gets an input stream to an image, given its URL. Used by {@link FlickrPhotosImporter} to
* upload the image.
*/
public BufferedInputStream get(String urlStr) throws IOException {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
return new BufferedInputStream(conn.getInputStream());
}
}
内容来源于网络,如有侵权,请联系作者删除!