2011-06-29 78 views
0

我手里有问题。获取url.getContent()时出现问题

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

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

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

我需要的只是这个$ 0中的一个URL。但直到现在我无法检索它。

Output when i inspect the element

在此$ 0,则一个变量名的网址,我试图把它牵回家。

我也很难理解这个$ 0和锄头来检索它。

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

After using strams to read the content

问候 Dheeraj乔希

回答

6

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

代码片段:

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

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

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

输出不可读的格式。 –

+0

@Dheeraj我添加了一个读取字节的例子。 – Marcelo

+0

我编辑我的问题引用什么输出后使用字节数组和字符串。 –

0

我找到了答案。

问题说明:当我执行一个URL时,响应中有另一个URL,我需要取回它。

解决方案:

java.net.URLConnection urlconn = url.openConnection(); 
java.net.HttpURLConnection conn = (java.net.HttpURLConnection)urlconn; 
conn.connect(); 
conn.getContent(); 

URL newurl = conn.getURL(); 
System.out.println(newurl.toString()); 

的响应可以是获得使用的getContent()和。连接对象将拥有包含新URL的委托。新的URL可以使用getURL方法获取。

问候
Dheeraj乔希

相关问题