好的,我最近問了一個關於在使用Java的情況下使用Web服務的問題,而我卻遇到了多個編譯錯誤。Java - 使用DefaultHttpClient消費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();
}
現在,我有一個url到.NET WSDL http://webservices.vm.vmc/ExampleService.asmx?WSDL
我想調用的方法「AddEmployee 「它接受5個字符串參數。
所以邏輯上我想:
- 改變我的HttpPost值是WSDL URL。
- 輸入方法名稱「AddEmployee」。 (不知道如何/在哪裏?)
- 我認爲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>
您使用哪種框架
的Java org.apache.axis.wsdl.WSDL2Java wsdlName消費Web服務軸或CXF? – NullPointerException
這是很好的知道。我已經將WSDL XML添加到我的OP中。您可以在DefaultHttpClient上提供的任何其他信息或我應該使用的信息都會有所幫助。謝謝! – adam
@NullPointerException我不知道這些是什麼。我還沒有實施任何框架。 – adam