java - 获取 url.getContent() 时出现问题

标签 java

我的手有问题。

我有一个 URL,当我启动到该 URL 的连接并执行 url.getContent() 时。 响应的类型为 sun.net.www.protocol.http.HttpURLConnection$HttpInputStream

我尝试将输出分配给HttpURLConnectionHttpInputStream h = url.getContent()。但我没有成功。 我将相应的库导入到代码中,但仍然没有成功。

如果我在 eclipse 中检查 url.getContent(),它还会显示其中的变量 thei$0。

我所需要的只是 this$0 中的一个 URL。但到目前为止我无法检索它。

Output when i inspect the element

在这个$0中有一个变量名称url,我正在尝试获取它。

我也很难理解这个$0并且很难检索它。

使用流后,我得到一些不可读的输出

After using strams to read the content

问候 迪拉吉·乔希

最佳答案

您应该使用openStream url 类的方法。

代码片段:

InputStream in = url.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = reader.readLine();

如果输出不是可读的字符串格式,则使用:

InputStream in = url.openStream();
byte[] buffer = new byte[512];
int bytesRead = in.read(buffer);

关于java - 获取 url.getContent() 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6523730/

相关文章:

java - Wikipedia API - 获取搜索结果的链接

java - 记录生成的 MIDI 事件 : inner conversion from timestamps to midi file delta time

java - jframe 中的 UnsupportedOperationException : Not supported yet.

Java 访问实例化的对象

java - setLocation() 并不总是适用于没有布局管理器的面板内的 JLabel 对象,这是一个错误吗?

java - 第一次执行夜间模式代码时,会触发当前重启

java - Jhipster - JpaRepository "principal.username"@Query - org.springframework.expression.spel.SpelEvaluationException

java - 无法连接到我的数据库 000webhost.com

java - Jar文件下载到缓存后只能打开一次

java - Strassen 算法的矩阵划分