- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我向特定 URL
发出 get
请求时,将下载一个文件。我可以从两种方式获取InputStream
。
方法一
使用java.net
包中的URL
类。
java.net.URL url = new URL(downloadFileUrl);
InputStream inputStream = url.openStream();
方法2
使用Apache
的HttpClient
类。
org.apache.http.impl.client.CloseableHttpClient httpclient = new CloseableHttpClient();
HttpGet request = new HttpGet(url);
CloseableHttpResponse response = httpclient.execute((HttpUriRequest)request);
InputStream inputStream = response.getEntity().getContent();
这些方法一样吗?如果不是怎么办?通常或在特定情况下首选哪种方法?
我提供的例子很简单。假设我做了必要的事情使用 URL
和 HttpClient
对象进行配置以获得成功响应。
最佳答案
这两种方法都会返回要从连接读取的输入流。这些方法之间没有区别。由于HttpClient是第三方库,因此您需要检查是否有任何漏洞并不断更新库。
唯一的区别是 HttpClient 仅支持 HTTP(s) 协议(protocol),而 URLConnection 也可用于其他协议(protocol),例如 FTP
在功能方面,Apache HttpClient比URLConnection有更多的微调选项
关于java - URL.openStream() 与 respone.getEntity().getContent() 相同吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56845773/
这个问题已经有答案了: Curly Brackets in Arrow Functions (3 个回答) 已关闭 5 年前。 这里是源代码: export class PostComponent i
我正在开发一个响应式网站,但我发现了一些问题。 我有一个包含图像和一些信息的 Div。当用户悬停此 div 时,它会更改背景并出现 3 个按钮。 但问题是:如果我使用手机并点击按钮位置上的 div(甚
我正在研究下载管理器项目我正在使用: public Stream GetStream(string url) { HttpWebRequest request = (HttpWebReques
我尝试调用一些 REST API 并处理 JSON 响应,阅读官方 Play 文档,我试试这个: CompletionStage token = ws.url("http://url.com")
当我向特定 URL 发出 get 请求时,将下载一个文件。我可以从两种方式获取InputStream。 方法一 使用java.net包中的URL类。 java.net.URL url = new UR
我在尝试写入文件时遇到问题。它是塔防游戏中的关卡编辑器。每次我单击“S”保存时,它都会卡住,当我单击显示屏时,它说没有响应,然后我必须使用任务管理器手动将其关闭。 package helpers; i
在解析过程中,我得到了带有 json 的文本对象响应。他们看起来都非常相似。其中一些工作没有任何错误。但其他人会抛出如下错误。 我尝试使用 replace('\r\n', '') 和 , strict
请帮忙:((失望) 我的 mysql 数据库中有一个表,用于存储 avalabe 读书时间; 在我的网站中,有一些按钮,用户可以点击并获取相关的可用图书时间,您可以在附图中看到。 每次用户点击左/右箭
我正在尝试从 onesignal api 发送 POST 请求 代码 axios({ method: 'post', url: 'https://onesignal.com/api/v1/no
我是一名优秀的程序员,十分优秀!