Garbled characters occur when retrieving WeChat user information and trusting user information

Source: Internet
Author: User
Tags openid

Garbled characters occur when retrieving user information.

When binding a user, you need to obtain the user information, which is prone to garbled characters.

        JSONObject jo = this.getAccessTokenOpenid(code);        StringBuilder sb = new StringBuilder("https://api.weixin.qq.com/sns/userinfo?access_token=");        sb.append(jo.getString("access_token"));        sb.append("&openid=").append(jo.getString("openid")).append("&lang=zh_CN");                HttpMethod method = new PostMethod(sb.toString());        HttpClient httpclient = new HttpClient();                httpclient.executeMethod(method);                        String result = new String(method.getResponseBody(), "utf-8");        //        String result = method.getResponseBodyAsString();
System.out.println("getWeiXinUserInfo result = " + result); JSONObject userInfo = JSON.parseObject(result, JSONObject.class);

Set String result =Method. getResponseBodyAsString ();

Change to String result =New String (method. getResponseBody (), "UTF-8");

You can.

Method. getResponseBodyAsString ():

Returns the response body of the HTTP method, if any, asString. If response body is not available or cannot be read,NullIs returned. The raw bytes in the body are converted toStringUsing the character encoding specified in the response'sContent-TypeHeader, or ISO-8859-1 if the response did not specify a character set.

Note that this method does not propagate I/O exceptions. If an error occurs while reading the body,nullWill be returned.

New String (method. getResponseBody (), "UTF-8 ")

Encode the native bytes returned by method. getResponseBody () with the specified UTF-8 encoding into a String. The returned value is UTF-8 encoding.

Running result:

No garbled characters appear.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.