Problem in getting url.getContent()

前端 未结 2 489
别跟我提以往
别跟我提以往 2020-12-22 12:24

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.protoc

相关标签:
2条回答
  • 2020-12-22 12:54

    You should 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, then use:

    InputStream in = url.openStream();
    byte[] buffer = new byte[512];
    int bytesRead = in.read(buffer);
    
    0 讨论(0)
  • 2020-12-22 12:57

    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

    0 讨论(0)
提交回复
热议问题