簡體   English   中英

Java - 使用DefaultHttpClient使用Web服務

[英]Java - Consume Web Service With DefaultHttpClient

好的,我最近問了一個關於使用Java來使用Web服務的問題,而我遇到了多個編譯錯誤。

我花了最后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

我想調用接受5個String參數的方法“AddEmployee”。

所以邏輯上我會:

  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>

有許多方法可以為Web服務生成客戶端。 如果您可以訪問wsdl(看起來像你這樣做),我建議使用像Apache Axis這樣的東西來為你生成客戶端類。

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

下載Axis后,您可以運行一個簡單的命令來生成客戶端類:

java org.apache.axis.wsdl.WSDL2Java wsdlName

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM