Problem getting url.getContent ()

I have a problem in my hand.

I have a url, and when I initiate a connection to that url and execute url.getContent (). The response is of type sun.net.www.protocol.http.HttpURLConnection$HttpInputStream

I tried to assign the output to HttpURLConnectionHttpInputStream h = url.getContent() . But I did not succeed. I imported the appropriate libraries for coding, but still no luck.

If I check url.getContent () in eclipse, it also shows the thei $ 0 variable in it.

All I need is the url in this 0 $. But so far I can’t get it back.

Output when i inspect the element

This $ 0 has a url of variable names, and I'm trying to extract it.

I am also having a hard time understanding this $ 0 and hoe to get it.

After using streams I get unreadable output

After using strams to read the content

Relationship Dheeraj Joshi

0
source share
2 answers

You must use the openStream method of the URL class.

Code snippet:

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

If the output is not in a readable string format, use:

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

I have found the answer.

Problem statement: when I execute the URL, the response had a different URL, and I needed to get it.

Decision:

 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()); 

The answer can be obtained using getContent () and. The connection object will have a delegate with a new URL. The new URL can be obtained using the getURL method.

respectfully
Dheeraj joshi

0
source

Source: https://habr.com/ru/post/911244/


All Articles