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