Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7023321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:41:45+00:00 2026-05-27T23:41:45+00:00

My task is to retrieve html strings from urls using Java. I know how

  • 0

My task is to retrieve html strings from urls using Java.

I know how to using HttpUrlConnection & InputStream to get the string.

However, I have an encoding problem for some pages.

If some pages have different encoding (e.g., GB2312), other than UTF8, the string I get is just arbitrary chars or question marks.

Can any one please tell me how to solve this problem?

Thanks

Below is my code to download the html from a url.

private String downloadHtml(String urlString) {
    URL url = null;
    InputStream inStr = null;
    StringBuffer buffer = new StringBuffer();

    try {
        url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // Cast shouldn't fail
        HttpURLConnection.setFollowRedirects(true);
        // allow both GZip and Deflate (ZLib) encodings
        //conn.setRequestProperty("Accept-Encoding", "gzip, deflate"); 
        String encoding = conn.getContentEncoding();
        inStr = null;

        // create the appropriate stream wrapper based on
        // the encoding type
        if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
            inStr = new GZIPInputStream(conn.getInputStream());
        } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
            inStr = new InflaterInputStream(conn.getInputStream(),
              new Inflater(true));
        } else {
            inStr = conn.getInputStream();
        }
        int ptr = 0;


        InputStreamReader inStrReader = new InputStreamReader(inStr, Charset.forName("GB2312"));

        while ((ptr = inStrReader.read()) != -1) {
            buffer.append((char)ptr);
        }
        inStrReader.close();

        conn.disconnect();
    }
    catch(Exception e) {

        e.printStackTrace();
    }
    finally {
        if (inStr != null)
            try {
                inStr.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }

    return buffer.toString();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:41:46+00:00Added an answer on May 27, 2026 at 11:41 pm

    By using an InputStreamReader and specifying your charset, like so:

    inStr = new InputStreamReader(InputStream, Charset.forName("GB2312"));
    

    The following code worked for me:

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.nio.charset.Charset;
    import java.util.zip.GZIPInputStream;
    import java.util.zip.Inflater;
    import java.util.zip.InflaterInputStream;
    
    public class Foo {
    
    public static void main(String[] args) {
        System.out.println(downloadHtml("http://baike.baidu.com/view/6000001.htm"));
    }
    
    
    private static String downloadHtml(String urlString) {
        URL url = null;
        InputStream inStr = null;
        StringBuffer buffer = new StringBuffer();
    
        try {
            url = new URL(urlString);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // Cast shouldn't fail
            HttpURLConnection.setFollowRedirects(true);
            // allow both GZip and Deflate (ZLib) encodings
            //conn.setRequestProperty("Accept-Encoding", "gzip, deflate"); 
            String encoding = conn.getContentEncoding();
            inStr = null;
    
            // create the appropriate stream wrapper based on
            // the encoding type
            if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
                inStr = new GZIPInputStream(conn.getInputStream());
            } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
                inStr = new InflaterInputStream(conn.getInputStream(),
                  new Inflater(true));
            } else {
                inStr = conn.getInputStream();
            }
            int ptr = 0;
    
    
            InputStreamReader inStrReader = new InputStreamReader(inStr, Charset.forName("GB2312"));
    
            while ((ptr = inStrReader.read()) != -1) {
                buffer.append((char)ptr);
            }
            inStrReader.close();
    
            conn.disconnect();
        }
        catch(Exception e) {
    
            e.printStackTrace();
        }
        finally {
            if (inStr != null)
                try {
                    inStr.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
    
        return buffer.toString();
      }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to retrieve the text data from an ePub file using Java.
How can I get the task_id value for a task from within the task?
Is there any way to retrieve the priority of a task using EWS Managed
My task is to retrieve an xml from https://test24.highrisehq.com/tasks/upcoming.xml that is a secure connection
I like to retrieve and store the values of an HTML table from a
The task is to retrieve parameters from session via JSTL. The session parameter name
my task is to retrieve a set of messages from GMail via POP3 (no
am using asynchronous task for hitting the web service url and retrieve the result
I have the following beans Task, ServerDetails and ApplicationDetails. I wish to retrieve all
My task is to decompress a packet(received) using zlib and then use an algoritm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.