title: Java - Consume Web Service With DefaultHttpClient
body: <p>Okay, I recently asked a question about consuming a Web Service using Java, while I was getting multiple compile errors.</p>
  
  <p>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.</p>
  
  <p>I now have the following code at least compiling without error.</p>
  
  <p><strong>Imports</strong></p>
  
  <pre><code>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></pre>
  
  <p><strong>Code</strong></p>
  
  <pre><code>    HttpClient client = new DefaultHttpClient();
      HttpPost post = new HttpPost("http://vogellac2dm.appspot.com/register");
  
      try {
          List&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(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();
      }
  </code></pre>
  
  <p>Now, I have a url to a .NET WSDL
  <a href="http://webservices.vm.vmc/ExampleService.asmx?WSDL" rel="nofollow">http://webservices.vm.vmc/ExampleService.asmx?WSDL</a></p>
  
  <p>And I want to invoke the method "AddEmployee" which accepts 5 String parameters.</p>
  
  <p>So logically I would:</p>
  
  <ol>
  <li>change my HttpPost value to be the WSDL URL.</li>
  <li>Put in the method name of "AddEmployee". <strong>(not sure how/where?)</strong></li>
  <li>I presume that the nameValuePairs.add would be used once per parameter(name,value)<strong>?</strong></li>
  </ol>
  
  <p>Thank you in advance for any help.</p>
  
  <hr>
  
  <hr>
  
  <p><strong>EDIT</strong></p>
  
  <p>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.</p>
  
  <pre><code>&lt;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"&gt;
    &lt;wsdl:types&gt;
      &lt;s:schema elementFormDefault="qualified" targetNamespace="https://webservices.vm.vmc/ClientSmart"&gt;
        &lt;s:element name="AddEmployee"&gt;
          &lt;s:complexType&gt;
            &lt;s:sequence&gt;
              &lt;s:element minOccurs="0" maxOccurs="1" name="string1" type="s:string"/&gt;
              &lt;s:element minOccurs="0" maxOccurs="1" name="string2" type="s:string"/&gt;
              &lt;s:element minOccurs="0" maxOccurs="1" name="string3" type="s:string"/&gt;
            &lt;/s:sequence&gt;
          &lt;/s:complexType&gt;
        &lt;/s:element&gt;
        &lt;s:element name="AddEmployeeResponse"&gt;
          &lt;s:complexType&gt;
            &lt;s:sequence&gt;
              &lt;s:element minOccurs="0" maxOccurs="1" name="AddEmployeeResult" type="s:string"/&gt;
            &lt;/s:sequence&gt;
          &lt;/s:complexType&gt;
        &lt;/s:element&gt;
      &lt;/s:schema&gt;
    &lt;/wsdl:types&gt;
    &lt;wsdl:message name="AddEmployeeSoapIn"&gt;
      &lt;wsdl:part name="parameters" element="tns:AddEmployee"/&gt;
    &lt;/wsdl:message&gt;
    &lt;wsdl:message name="AddEmployeeSoapOut"&gt;
      &lt;wsdl:part name="parameters" element="tns:AddEmployeeResponse"/&gt;
    &lt;/wsdl:message&gt;
    &lt;wsdl:portType name="ExampleServiceSoap"&gt;
      &lt;wsdl:operation name="AddEmployee"&gt;
        &lt;wsdl:input message="tns:AddEmployeeSoapIn"/&gt;
        &lt;wsdl:output message="tns:AddEmployeeSoapOut"/&gt;
      &lt;/wsdl:operation&gt;
    &lt;/wsdl:portType&gt;
    &lt;wsdl:binding name="ExampleServiceSoap" type="tns:ExampleServiceSoap"&gt;
      &lt;soap:binding transport="http://schemas.xmlsoap.org/soap/http"/&gt;
      &lt;wsdl:operation name="AddEmployee"&gt;
        &lt;soap:operation soapAction="https://webservices.vm.vmc/ClientSmart/AddEmployee" style="document"/&gt;
        &lt;wsdl:input&gt;
          &lt;soap:body use="literal"/&gt;
        &lt;/wsdl:input&gt;
        &lt;wsdl:output&gt;
          &lt;soap:body use="literal"/&gt;
        &lt;/wsdl:output&gt;
      &lt;/wsdl:operation&gt;
    &lt;/wsdl:binding&gt;
    &lt;wsdl:binding name="ExampleServiceSoap12" type="tns:ExampleServiceSoap"&gt;
      &lt;soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/&gt;
      &lt;wsdl:operation name="AddEmployee"&gt;
        &lt;soap12:operation soapAction="https://webservices.vm.vmc/ClientSmart/AddEmployee" style="document"/&gt;
        &lt;wsdl:input&gt;
          &lt;soap12:body use="literal"/&gt;
        &lt;/wsdl:input&gt;
        &lt;wsdl:output&gt;
          &lt;soap12:body use="literal"/&gt;
        &lt;/wsdl:output&gt;
      &lt;/wsdl:operation&gt;
    &lt;/wsdl:binding&gt;
    &lt;wsdl:service name="ExampleService"&gt;
      &lt;wsdl:port name="ExampleServiceSoap" binding="tns:ExampleServiceSoap"&gt;
        &lt;soap:address location="http://localhost:4729/POSWebServices/ExampleService.asmx"/&gt;
      &lt;/wsdl:port&gt;
      &lt;wsdl:port name="ExampleServiceSoap12" binding="tns:ExampleServiceSoap12"&gt;
        &lt;soap12:address location="http://localhost:4729/POSWebServices/ExampleService.asmx"/&gt;
      &lt;/wsdl:port&gt;
    &lt;/wsdl:service&gt;
  &lt;/wsdl:definitions&gt;
  </code></pre>