客户端请求 SOAP 示例
我在服务器上部署了 HelloWorld Web 服务。现在我正在尝试与服务器交谈。如何从 Java 应用程序中发出 SOAP 请求?
这是 XML 信封(请求):
POST /AndroidSampleApp/Test.asmx HTTP/1.1
Host: (host)
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/HelloWorld"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<HelloWorld xmlns="http://tempuri.org/">
<username>string</username>
<password>string</password>
</HelloWorld>
</soap:Body>
</soap:Envelope>
这是 XML 信封(请求):
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<HelloWorldResponse xmlns="http://tempuri.org/">
<HelloWorldResult>string</HelloWorldResult>
</HelloWorldResponse>
</soap:Body>
</soap:Envelope>
我如何从 java 程序调用此 Web 服务以及我需要导入哪些包。谢谢!
注意:这是针对 Android 应用程序的
I have a HelloWorld web service deployed on a server. Now im trying to talk to the server. How do i do a SOAP request from within a Java application ?
Here is the XML envelope (request):
POST /AndroidSampleApp/Test.asmx HTTP/1.1
Host: (host)
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/HelloWorld"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<HelloWorld xmlns="http://tempuri.org/">
<username>string</username>
<password>string</password>
</HelloWorld>
</soap:Body>
</soap:Envelope>
Here is the XML envelope (request):
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<HelloWorldResponse xmlns="http://tempuri.org/">
<HelloWorldResult>string</HelloWorldResult>
</HelloWorldResponse>
</soap:Body>
</soap:Envelope>
How do i call this webservice from a java program and what packages do i need to import. Thanks!
Note: This is for an android application
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 ksoap2 库轻松进行肥皂网络服务调用。
Ksoap 相对容易使用,但我在使用它的某些网络上遇到了一些连接性能问题。
相反,我一直在使用 HttpUrlConnection (或 AndroidHttpClient,具体取决于操作系统版本)。请参阅此帖子了解更多信息。我将它们与 Android 上可用的任何 XML 解析器和构建器结合使用。
希望这有帮助!
You can use the ksoap2 library to easily make your soap webservie calls.
Ksoap is relatively easy to use, but I've run into some performance issues with connectivity on some networks using it.
Instead, I've been using HttpUrlConnection (or AndroidHttpClient, depending on the OS version). See this post for more information. I use these in combination with any of the XML parsers and builders available on Android.
Hope this helps!
如果您想在 Java 中调用它,那么一种方法是让soapUI 为您生成一个客户端 jar。
http://2mohitarora.blogspot.com /2010/11/how-to-generate-web-service-client-jar.html
如果你使用的是Apache cxf,你可以看看这篇文章如何生成客户端:
http://logicsector.wordpress.com/2008/10/19/how-to-create-a-wsdl-first-soap-client-in-java-with-cxf-and-maven/
If you want to call it in Java, then one way to do it is to have soapUI generate a client jar for you.
http://2mohitarora.blogspot.com/2010/11/how-to-generate-web-service-client-jar.html
If you are using Apache cxf, you can look at this article how to generate a client:
http://logicsector.wordpress.com/2008/10/19/how-to-create-a-wsdl-first-soap-client-in-java-with-cxf-and-maven/