我在Java中创建了一个我计划在Tomcat服务器上部署的Web服务。我打算从Windows应用商店应用程序中使用此服务。不幸的是,我继续在服务器端和客户端都获得一些例外。
这是我正在努力实现的非常简化的实现。
Java Web服务
依赖关系:在https://jax-ws.java.net/2.2.8/
找到JAX-WS 2.2.8版我实现了一个简单的'Hello'世界java服务。我使用发布者类来测试服务,而不涉及Tomcat。不过,我已经尝试过Tomcat中的服务,无论我如何部署它,错误都是一致的。
HelloWorld代码:
package com.test.service;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
@WebMethod
public String helloWorld(String name);
}
HelloWorldImpl代码:
package com.test.service;
import javax.jws.WebService;
@WebService(endpointInterface="com.test.service.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
public String helloWorld(String name) {
return "Hello " + name;
}
}
发布商代码:
package com.test.service;
import javax.xml.ws.Endpoint;
public class Publisher {
public static void main(String[] args) {
Endpoint.publish("http://localhost:8082/TEST/HelloWorld",new HelloWorldImpl());
}
}
以下是它生成的wsdl:
<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6. -->
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6. -->
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://service.test.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://schemas.xmlsoap.org/wsdl/"targetNamespace="http://service.test.com/"
name="HelloWorldImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://service.test.com/"
schemaLocation="http://localhost:8082/TEST/HelloWorld?xsd=1"/>
</xsd:schema>
</types>
<message name="helloWorld">
<part name="parameters" element="tns:helloWorld"/>
</message>
<message name="helloWorldResponse">
<part name="parameters" element="tns:helloWorldResponse"/>
</message>
<portType name="HelloWorld">
<operation name="helloWorld">
<input message="tns:helloWorld"/>
<output message="tns:helloWorldResponse"/>
</operation>
</portType>
<binding name="HelloWorldImplPortBinding" type="tns:HelloWorld">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="helloWorld">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="HelloWorldImplService">
<port name="HelloWorldImplPort" binding="tns:HelloWorldImplPortBinding">
<soap:address location="http://localhost:8082/TEST/HelloWorld"/>
</port>
</service>
</definitions>
客户服务消费者
依赖项:Microsoft Visual Studio 2013
在客户端,我首先实现了一个简单的“Windows窗体”应用程序来测试服务。以下是我用于添加服务的步骤:
或者,我可以尝试通过单击“高级”按钮添加“Web服务”,然后在打开的对话框中单击“添加Web引用”。没关系,两种方式都可以。
以下是调用服务的代码行:
HelloService.HelloWorldImplService service = new HelloService.HelloWorldImplService();
System.Diagnostics.Debug.WriteLine(service.helloWorld("World!"));
以上示例打印出“Hello World!”
现在我想在Windows应用商店应用中尝试一下。所以我创建了一个应用程序,然后继续添加服务。就像Windows表单应用程序一样,我添加了以下服务。
它生成的代码与“Windows Form”应用程序生成的代码略有不同。这些方法创建了等待响应的任务。以下是调用服务的代码:
HelloService.HelloWorldClient service = new HelloService.HelloWorldClient();
System.Threading.Tasks.Task<HelloService.helloWorldResponse> task = service.helloWorldAsync("World!");
task.Wait();
System.Diagnostics.Debug.WriteLine(task.Result.ToString());
不幸的是,这段代码不起作用。首先,我在服务器端遇到异常。
Dec 8, 2013 1:00:12 PM com.sun.xml.internal.ws.transport.http.HttpAdapter$HttpToolkit handle
SEVERE: Unsupported Content-Type: application/soap+xml; charset=utf-8 Supported ones are: [text/xml]
com.sun.xml.internal.ws.server.UnsupportedMediaException: Unsupported Content-Type: application/soap+xml; charset=utf-8 Supported ones are: [text/xml]
at com.sun.xml.internal.ws.encoding.StreamSOAPCodec.decode(StreamSOAPCodec.java:284)
at com.sun.xml.internal.ws.encoding.StreamSOAPCodec.decode(StreamSOAPCodec.java:118)
at com.sun.xml.internal.ws.encoding.SOAPBindingCodec.decode(SOAPBindingCodec.java:278)
at com.sun.xml.internal.ws.transport.http.HttpAdapter.decodePacket(HttpAdapter.java:266)
at com.sun.xml.internal.ws.transport.http.HttpAdapter.access$500(HttpAdapter.java:82)
at com.sun.xml.internal.ws.transport.http.HttpAdapter$HttpToolkit.handle(HttpAdapter.java:446)
at com.sun.xml.internal.ws.transport.http.HttpAdapter.handle(HttpAdapter.java:233)
at com.sun.xml.internal.ws.transport.http.server.WSHttpHandler.handleExchange(WSHttpHandler.java:95)
at com.sun.xml.internal.ws.transport.http.server.WSHttpHandler.handle(WSHttpHandler.java:80)
at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:83)
at sun.net.httpserver.AuthFilter.doFilter(AuthFilter.java:83)
at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:86)
at sun.net.httpserver.ServerImpl$Exchange$LinkHandler.handle(ServerImpl.java:598)
at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:83)
at sun.net.httpserver.ServerImpl$Exchange.run(ServerImpl.java:570)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:701)
这告诉我服务器在客户端使用SOAP 1.2时期望SOAP 1.1。
在客户端,我收到以下消息。
InnerException: System.ServiceModel.EndpointNotFoundException
HResult=-2146233087
Message=There was no endpoint listening at http://localhost:8082/TEST/HelloWorld that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
Source=System.ServiceModel.Internals
StackTrace:
at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)
at System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result)
at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
at System.ServiceModel.Channels.ServiceChannelProxy.TaskCreator.<>c__DisplayClass5`1.<CreateGenericTask>b__4(IAsyncResult asyncResult)
at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
InnerException: System.Net.WebException
HResult=-2146233079
Message=Unable to connect to the remote server
Source=System
StackTrace:
at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context)
at System.ServiceModel.Channels.HttpOutput.WebRequestHttpOutput.GetOutputStreamAsyncResult.CompleteGetRequestStream(IAsyncResult result)
InnerException: System.Net.Sockets.SocketException
HResult=-2147467259
Message=An attempt was made to access a socket in a way forbidden by its access permissions 192.168.1.100:8082
Source=System
ErrorCode=10013
NativeErrorCode=10013
StackTrace:
at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
这是有道理的,因为Web服务无法处理请求。
无论如何,我花了好几天试图找出这个问题,但似乎无法找到解决方案。
所以这是我的问题......