How to solve the problem of webservice parameter passing Chinese garbled characters

Source: Internet
Author: User

1, the origin of the problem

My main job is to be responsible for the unified adaptation platform, the main task is to provide customers with a unified call interface, customers use this interface can be used by us to adjust the interface of different manufacturers. The main processes are as follows:

Now the customer has come from the request message contained in Chinese, using webservice send is generally used UTF-8, but the client side of the request from the format is certainly not, otherwise it will not appear in Chinese garbled:

2, the solution of the law

In the process of northbound processing, the code is converted when a message is received from the customer. With the following two methods, you only need to call the first method to convert a message when you get a request.

    //Chinese character handling     Publicstring Charsetconvert (String xmlrequest) {string CharSet=neopserverimpl.getencoding (xmlrequest); Try {            byte[] B =xmlrequest.getbytes (CharSet); XMLRequest=NewString (b, "UTF-8"); } Catch(Exception e) {logger.error ("The input is not part of the common encoding format, please check again carefully", E); }        returnXMLRequest; }     Public Staticstring GetEncoding (String str) {string encode= "GB2312"; Try {            if(Str.equals (NewString (Str.getbytes (encode), encode))) {//Judging is not GB2312String s =encode; returnS//If yes, return to GB2312, the following code is the same            }        } Catch(Exception e) {logger.error ("GetEncoding Exception---GB2312", E); } Encode= "Iso-8859-1"; Try {            if(Str.equals (NewString (Str.getbytes (encode), encode))) {//Judging is not iso-8859-1String S1 =encode; returnS1; }        } Catch(Exception e) {logger.error ("GetEncoding Exception---iso-8859-1", E); } Encode= "UTF-8"; Try {            if(Str.equals (NewString (Str.getbytes (encode), encode))) {//Judging is not UTF-8 encodedString s2 =encode; returnS2; }        } Catch(Exception e) {logger.error ("GetEncoding Exception---UTF-8", E); } Encode= "GBK"; Try {            if(Str.equals (NewString (Str.getbytes (encode), encode))) {//Judging is not GBKString s3 =encode; returnS3; }        } Catch(Exception e) {logger.error ("GetEncoding Exception---GBK", E); }        return"";//in this step, you should check for other codes.
    }

Note: The above part of the Code reference online resources: http://zhidao.baidu.com/link?url= gxafos7hiake8lcuuldtw9sqwa3c3vnllbfid09ly2wyo8ypum0yc5bfgkfy5vapuajbli4qkr5cdbe0_ewe0_

How to solve the problem of webservice parameter passing Chinese garbled characters

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.