基于jws的webservice客户端调用方法时出现中文乱码
时间: 2023-06-24 07:04:05 浏览: 144
中文乱码通常是由于字符编码不一致导致的。解决方法如下:
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文本格式。这样可以确保服务端和客户端使用相同的字符编码,避免中文乱码问题。
阅读全文