WebService 客户端获取别的服务器端传递xml或者json中文乱码

最近在对接某行的网站后台WebService接口,对接过程中出现的乱码做下记录

使用WebService对接方式是:JAX-WS 方式。

开发工具 myeclipse 新建客户端 

依次按照步骤 生成java文件:

编写测试类

以上能与对方接口进行调用,但是返回字符串出现中文乱码,

经查发现在问题出现在加密这个方法中,由于对方是使用GBK进行编码,本项目使用AES-128-ECB加密算法,在加解密时:

加密:Cipher 的doFinal(srcString.getBytes("GBK")); 要这么处理;

解密:也要对doFinal进行的处理。

问题解决,以上是本人在项目中遇到的问题,如对您没有帮助,请忽略。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
中文乱码通常是由于字符编码不一致导致的。解决方法如下: 1. 在客户端设置字符编码为UTF-8(或与服务端相同的编码),例如: ```java String encoding = "UTF-8"; BindingProvider provider = (BindingProvider) port; Map<String, Object> requestContext = provider.getRequestContext(); requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpoint); requestContext.put(BindingProvider.USERNAME_PROPERTY, username); requestContext.put(BindingProvider.PASSWORD_PROPERTY, password); requestContext.put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true); requestContext.put(MessageContext.HTTP_REQUEST_HEADERS, headers); requestContext.put(JAXWSProperties.HTTP_CLIENT_STREAMING_CHUNK_SIZE, 8192); requestContext.put(BindingProviderProperties.CONNECT_TIMEOUT, 3000); requestContext.put(BindingProviderProperties.REQUEST_TIMEOUT, 3000); requestContext.put(JAXWSProperties.HTTP_RESPONSE_TIMEOUT, 3000); requestContext.put(JAXWSProperties.HTTP_CLIENT_ALLOW_CHUNKING, true); requestContext.put(JAXWSProperties.HTTP_CLIENT_STREAMING_CHUNK_SIZE, 8192); requestContext.put(JAXWSProperties.CONNECT_TIMEOUT, 3000); requestContext.put(JAXWSProperties.REQUEST_TIMEOUT, 3000); requestContext.put(JAXWSProperties.HTTP_RESPONSE_TIMEOUT, 3000); requestContext.put(JAXWSProperties.HTTP_CLIENT_ALLOW_CHUNKING, true); requestContext.put(JAXWSProperties.HTTP_CLIENT_STREAMING_CHUNK_SIZE, 8192); requestContext.put(BindingProviderProperties.CONNECT_TIMEOUT, 3000); requestContext.put(BindingProviderProperties.REQUEST_TIMEOUT, 3000); requestContext.put(JAXWSProperties.HTTP_RESPONSE_TIMEOUT, 3000); requestContext.put(JAXWSProperties.HTTP_CLIENT_ALLOW_CHUNKING, true); requestContext.put(JAXWSProperties.HTTP_CLIENT_STREAMING_CHUNK_SIZE, 8192); //设置字符编码 requestContext.put(BindingProviderProperties.ENCODING, encoding); ``` 2. 在服务端设置字符编码为UTF-8(或与客户端相同的编码),例如: ```xml <?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://localhost:8080/HelloWorld" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://localhost:8080/HelloWorld"> <types> <xsd:schema> <xsd:import namespace="http://localhost:8080/HelloWorld" schemaLocation="http://localhost:8080/HelloWorld?xsd=1"/> </xsd:schema> </types> <message name="sayHello"> <part name="name" type="xsd:string"/> </message> <message name="sayHelloResponse"> <part name="return" type="xsd:string"/> </message> <portType name="HelloWorld"> <operation name="sayHello"> <input message="tns:sayHello"/> <output message="tns:sayHelloResponse"/> </operation> </portType> <binding name="HelloWorldSoapBinding" type="tns:HelloWorld"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="sayHello"> <soap:operation soapAction=""/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> </binding> <service name="HelloWorldService"> <port name="HelloWorldPort" binding="tns:HelloWorldSoapBinding"> <soap:address location="http://localhost:8080/HelloWorld"/> </port> </service> </definitions> ``` 在这个例子,我们在WSDL文件头部设置了编码为UTF-8,并在SOAP消息的body元素使用了use="literal"来指定使用XML文本格式。这样可以确保服务端和客户端使用相同的字符编码,避免中文乱码问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值