0

I have a problem in my hand.

I have a URL, And when i initiate the connect to this 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 was unsuccessful. I imported corresponding libraries to code, but still no luck.

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

All i need is a URL in this$0. But till now i am unable to retreive it.

Output when i inspect the element

In this$0 there is a variable names url and i am trying to fetch it.

I also have hard time understand this$0 and hoe to retrieve it.

After using the streams i get some non readable output

After using strams to read the content

Regards Dheeraj Joshi

2 Answers 2

6

You should use the openStream method of the url class.

Code snippet:

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

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

InputStream in = url.openStream();
byte[] buffer = new byte[512];
int bytesRead = in.read(buffer);
Sign up to request clarification or add additional context in comments.

4 Comments

I edited my question quoting whats the output after using both byte array and string.
@Dheeraj I really have no idea about the URL that you are opening and the content that you are supposed to fetch, my snippets will help you fetch the content. If you think its not readable, its because you don't know the specification of the content that you are supposed to read. I can not help you with that.
Isn't there any other method to get the object of the response. and try to fetch the URL? Since the streams do not really help here?
u forgot to add the in on the InputStreamReader constructor
1

I found the answer.

The problem statement: When i execute a URL the response had another URL in it and i needed to fetch it.

Solution:

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 response can be get using getContent() and. The connection object will have a delegate with the new URL. The new URL can be fetched using getURL method.

Regards
Dheeraj Joshi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.