简体   繁体   中英

Java - Consume Web Service With DefaultHttpClient

Okay, I recently asked a question about consuming a Web Service using Java, while I was getting multiple compile errors.

I have literally spent the last 7 hours trying to get this working, doing research online. But Java web service calls come in so many different flavors it's proving almost impossible for a novice such as myself to find relevant information for my scenario.

I now have the following code at least compiling without error.

Imports

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;

Code

    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();
    }

Now, I have a url to a .NET WSDL http://webservices.vm.vmc/ExampleService.asmx?WSDL

And I want to invoke the method "AddEmployee" which accepts 5 String parameters.

So logically I would:

  1. change my HttpPost value to be the WSDL URL.
  2. Put in the method name of "AddEmployee". (not sure how/where?)
  3. I presume that the nameValuePairs.add would be used once per parameter(name,value) ?

Thank you in advance for any help.



EDIT

Without disclosing any detailed information, I've created an example of what I want to call. Here is the WSDL for a simple service, which has 1 method, accepts 3 string parameters, and returns a string.

<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>

There are a number of ways to generate a client for a Web Service. If you have access to the wsdl (which it looks like you do) I would recommend using something like Apache Axis to generate the client classes for you.

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

Once you've downloaded Axis you can run a simple command to generate the client classes:

java org.apache.axis.wsdl.WSDL2Java wsdlName

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM