Uploaded image for project: 'HttpComponents HttpClient'
  1. HttpComponents HttpClient
  2. HTTPCLIENT-1175

URLEncodedUtils.parse(HttpEntity) not working if Content-Encoding is not specified

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Invalid
    • Affects Version/s: 4.1.3, 4.2 Beta1
    • Fix Version/s: None
    • Component/s: HttpClient (classic)
    • Labels:
      None

      Description

      The code below does not work :

      /**

      • Sets the authCode received from Facebook and exchanges it with the access token.
      • @param authCode
      • @throws IOException
      • @throws ClientProtocolException
        */
        public void setFacebookAuthCode(String authCode) throws ClientProtocolException, IOException {
        log.info("Retrieving access token using authCode {}", authCode);
        URI accessTokenUri = facebookAuth.getAccessTokenUri(authCode);
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet accessTokenReq = new HttpGet(accessTokenUri);
        HttpResponse response = client.execute(accessTokenReq);
        if (response.getStatusLine().getStatusCode() != 200)
        throw new IOException(String.format("GET %s throws HTTP Error %d: %s",
        accessTokenUri, response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()));
        // Probably due to non-existing Content-Encoding, this one is not working:
        List<NameValuePair> data = URLEncodedUtils.parse(response.getEntity());
        setFbAccessToken(data.get(0).getValue());

      log.info("Access token received, redirecting to Post Status page");
      ExternalContext external = FacesContext.getCurrentInstance().getExternalContext();
      external.redirect(external.encodeActionURL("/faces/post.xhtml"));
      }

      I have to use a workaround :

      Scanner scanner = new Scanner(response.getEntity().getContent());
      ArrayList<NameValuePair> data = new ArrayList<NameValuePair>();
      URLEncodedUtils.parse(data, scanner, "UTF-8");

      Expected behavior :

      Should either default to UTF-8 (recommended) or make it an explicit parameter.

      FYI : Code above is taken from FBStatus open source project at https://github.com/soluvas/fbstatus

        Activity

        Hide
        olegk Oleg Kalnichevski added a comment -

        Why do you think the method should default to UTF-8 and not ISO-8859-1, which is the default charset for text received via HTTP per HTTP spec?

        By the way, the workaround you mentioned is not a workaround. This is precisely what one is supposed to do when wanting to force a custom charset over the one specified in the Content-Type header (or the default one in case Content-Type is not present).

        Oleg

        Show
        olegk Oleg Kalnichevski added a comment - Why do you think the method should default to UTF-8 and not ISO-8859-1, which is the default charset for text received via HTTP per HTTP spec? By the way, the workaround you mentioned is not a workaround. This is precisely what one is supposed to do when wanting to force a custom charset over the one specified in the Content-Type header (or the default one in case Content-Type is not present). Oleg
        Hide
        ceefour Hendy Irawan added a comment -

        Sorry. If ISO-8859-1 is the default one per HTTP spec, then I also agree with it.

        The "workaround" seems to be a real workaround starting with 4.2.x, because parse(String, Scanner, String) is marked as deprecated.

        Show
        ceefour Hendy Irawan added a comment - Sorry. If ISO-8859-1 is the default one per HTTP spec, then I also agree with it. The "workaround" seems to be a real workaround starting with 4.2.x, because parse(String, Scanner, String) is marked as deprecated.
        Hide
        olegk Oleg Kalnichevski added a comment -

        There is now #parse method that can parse URL coded content from a String. I just realized, though, that Scanner could actually work with input streams, not just strings. This certainly makes it quite useful. I'll un-deprecate the method in SVN trunk.

        Oleg

        Show
        olegk Oleg Kalnichevski added a comment - There is now #parse method that can parse URL coded content from a String. I just realized, though, that Scanner could actually work with input streams, not just strings. This certainly makes it quite useful. I'll un-deprecate the method in SVN trunk. Oleg
        Hide
        ceefour Hendy Irawan added a comment -

        Thank you Oleg ! Much appreciated

        Show
        ceefour Hendy Irawan added a comment - Thank you Oleg ! Much appreciated

          People

          • Assignee:
            Unassigned
            Reporter:
            ceefour Hendy Irawan
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development