【问题标题】:Java - Consume Web Service With DefaultHttpClientJava - 使用 DefaultHttpClient 使用 Web 服务
【发布时间】:2013-04-16 05:38:55
【问题描述】:

好的,我最近问了一个关于使用 Java 使用 Web 服务的问题,同时遇到多个编译错误。

在过去的 7 个小时里,我确实花了 7 个小时试图让这项工作正常进行,并在网上进行研究。但是 Java Web 服务调用有很多不同的风格,事实证明,像我这样的新手几乎不可能找到与我的场景相关的信息。

我现在至少可以正确编译以下代码。

进口

import java.util.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

代码

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://vogellac2dm.appspot.com/register");

    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("registrationid", "123456789"));
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = client.execute(post);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String line = "";
        while ((line = rd.readLine()) != null) {
            System.out.println(line);
        }

    } catch (IOException e) {
        e.printStackTrace();
    }

现在,我有一个 .NET WSDL 的 url http://webservices.vm.vmc/ExampleService.asmx?WSDL

我想调用“AddEmployee”方法,它接受 5 个字符串参数。

所以逻辑上我会:

  1. 将我的 HttpPost 值更改为 WSDL URL。
  2. 输入“AddEmployee”的方法名。 (不确定如何/在哪里?)
  3. 我假设 nameValuePairs.add 将在每个参数(名称,值)中使用一次

提前感谢您的帮助。



编辑

在没有透露任何详细信息的情况下,我创建了一个我想要调用的示例。这是一个简单服务的 WSDL,它有 1 个方法,接受 3 个字符串参数,并返回一个字符串。

<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="https://webservices.vm.vmc/ClientSmart" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetNamespace="https://webservices.vm.vmc/ClientSmart">
  <wsdl:types>
    <s:schema elementFormDefault="qualified" targetNamespace="https://webservices.vm.vmc/ClientSmart">
      <s:element name="AddEmployee">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="string1" type="s:string"/>
            <s:element minOccurs="0" maxOccurs="1" name="string2" type="s:string"/>
            <s:element minOccurs="0" maxOccurs="1" name="string3" type="s:string"/>
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="AddEmployeeResponse">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="AddEmployeeResult" type="s:string"/>
          </s:sequence>
        </s:complexType>
      </s:element>
    </s:schema>
  </wsdl:types>
  <wsdl:message name="AddEmployeeSoapIn">
    <wsdl:part name="parameters" element="tns:AddEmployee"/>
  </wsdl:message>
  <wsdl:message name="AddEmployeeSoapOut">
    <wsdl:part name="parameters" element="tns:AddEmployeeResponse"/>
  </wsdl:message>
  <wsdl:portType name="ExampleServiceSoap">
    <wsdl:operation name="AddEmployee">
      <wsdl:input message="tns:AddEmployeeSoapIn"/>
      <wsdl:output message="tns:AddEmployeeSoapOut"/>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="ExampleServiceSoap" type="tns:ExampleServiceSoap">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="AddEmployee">
      <soap:operation soapAction="https://webservices.vm.vmc/ClientSmart/AddEmployee" style="document"/>
      <wsdl:input>
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:binding name="ExampleServiceSoap12" type="tns:ExampleServiceSoap">
    <soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="AddEmployee">
      <soap12:operation soapAction="https://webservices.vm.vmc/ClientSmart/AddEmployee" style="document"/>
      <wsdl:input>
        <soap12:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap12:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="ExampleService">
    <wsdl:port name="ExampleServiceSoap" binding="tns:ExampleServiceSoap">
      <soap:address location="http://localhost:4729/POSWebServices/ExampleService.asmx"/>
    </wsdl:port>
    <wsdl:port name="ExampleServiceSoap12" binding="tns:ExampleServiceSoap12">
      <soap12:address location="http://localhost:4729/POSWebServices/ExampleService.asmx"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

【问题讨论】:

  • 您使用哪个框架来使用 webservices 轴或 CXF ?
  • 很高兴知道这一点。我已将 WSDL XML 添加到我的 OP 中。您可以在 DefaultHttpClient 上提供的任何进一步信息或我应该使用的信息都会有所帮助。谢谢!
  • @NullPointerException 我不知道其中任何一个是什么。我还没有实现任何框架。
  • 使用框架来使用网络服务。看下面链接cxf.apache.org/docs/developing-a-consumer.html

标签: java web-services http jakarta-ee soap


【解决方案1】:

有多种方法可以为 Web 服务生成客户端。如果您可以访问 wsdl(看起来像您一样),我建议您使用 Apache Axis 之类的东西为您生成客户端类。

http://axis.apache.org/axis/java/index.html

下载 Axis 后,您可以运行一个简单的命令来生成客户端类:

java org.apache.axis.wsdl.WSDL2Java wsdlName

【讨论】:

  • 所以我可以下载该工具...它将生成一些我可以放置在项目中的自定义类,并且我不需要在服务器上安装任何工具?
  • @adam 您必须将 apache 轴客户端 jar 添加到您的项目中。
  • 有什么理由我应该选择 Axis 而不是 Axis2? axis.apache.org/axis2/java/core
  • 我什至会选择 jdks 中包含的标准 java-sdk Web 服务客户端工具>=1.6。无需将axis之类的东西拖入客户端(wsimport等都包含在JDK中,API在标准SE JRE中)。
猜你喜欢
  • 1970-01-01
  • 2013-11-08
  • 2012-07-24
  • 1970-01-01
  • 1970-01-01
  • 2016-09-05
  • 1970-01-01
  • 1970-01-01
  • 2011-01-12
相关资源
最近更新 更多