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 6094669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:42:23+00:00 2026-05-23T12:42:23+00:00

I have a following class public class MyHttpClient { private static HttpClient httpClient =

  • 0

I have a following class

public class MyHttpClient {
private static HttpClient httpClient = null;

public static HttpClient getHttpClient() {
    if (httpClient == null)
        httpClient = new DefaultHttpClient();

    return httpClient;
}

public static String HttpGetRequest(String url) throws IOException {
    HttpGet request = new HttpGet(url);
    HttpResponse response = null;
    InputStream stream = null;

    String result = "";
    try {
        response = getHttpClient().execute(request);
        if (response.getStatusLine().getStatusCode() != 200)
            response = null;
        else
            stream = response.getEntity().getContent();

            String line = "";
            StringBuilder total = new StringBuilder();
            BufferedReader rd = new BufferedReader(new InputStreamReader(stream));
            while ((line = rd.readLine()) != null) { 
                total.append(line); 
            }

            // Return full string
            result = total.toString();
    } catch (ClientProtocolException e) {
        response = null;
        stream = null;
        result = null;
    } catch (IllegalStateException e) {
        response = null;
        stream = null;
        result = null;
    }

    return result;
}
}

and a web-service which response header is (I can’t provide direct link because of privacy)

Status: HTTP/1.1 200

OK Cache-Control: private

Content-Type: application/json;

charset=utf-8

Content-Encoding: gzip

Server: Microsoft-IIS/7.5

X-AspNetMvc-Version: 3.0

X-AspNet-Version: 4.0.30319

X-Powered-By: ASP.NET

Date: Sun, 03

Jul 2011 11:00:43 GMT

Connection: close

Content-Length: 8134

In the end I get as a result series of weird, unreadable characters (I should get regular JSON like I get in regular desktop browser).

Where is the problem? (The same code for ex. google.com works perfectly and I get nice result)

EDIT: Solution (see below for description)
Replace

HttpGet request = new HttpGet(url);

with

HttpUriRequest request = new HttpGet(url);
request.addHeader("Accept-Encoding", "gzip");

and replace

stream = response.getEntity().getContent();

with

stream = response.getEntity().getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
    stream = new GZIPInputStream(stream);
}
  • 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-23T12:42:24+00:00Added an answer on May 23, 2026 at 12:42 pm

    The problem is here:

    Content-Encoding: gzip
    

    This means the weird characters you are getting are the gzip compressed version of the expected JSON. Your browser does the decmpression so there you see the decoded result. You should have a look at your request header and at the configuration of your server.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following class: public abstract class AbstractParent { static String method() {
So I have the following: public class Singleton { private Singleton(){} public static readonly
I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int
If have following class public abstract class MyBaseClass : BaseClass { public override string
I have the following class public class UIControl { public string FName{ get; set;
I have the following class: public class Fluently { public Fluently Is(string lhs) {
I have the following class: public class Topic { public string Topic { get;
I have the following class: public class SqlCeEventStore: EventStore { private EventStoreDB db; public
I have the following class public class Car { public Name {get; set;} }
Suppose I have the following class: public class TestBase { public bool runMethod1 {

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.