Java-使用DefaultHttpClient使用Web服务
好的,我最近问了一个关于使用Java消费Web服务的问题,当时我遇到了多个编译错误 在过去的7个小时里,我一直在网上做研究,试图让这项技术发挥作用。但是Java web服务调用有很多不同的风格,对于像我这样的新手来说,几乎不可能找到与我的场景相关的信息 我现在有以下代码,至少编译时没有错误 导入Java-使用DefaultHttpClient使用Web服务,java,web-services,http,jakarta-ee,soap,Java,Web Services,Http,Jakarta Ee,Soap,好的,我最近问了一个关于使用Java消费Web服务的问题,当时我遇到了多个编译错误 在过去的7个小时里,我一直在网上做研究,试图让这项技术发挥作用。但是Java web服务调用有很多不同的风格,对于像我这样的新手来说,几乎不可能找到与我的场景相关的信息 我现在有以下代码,至少编译时没有错误 导入 import java.util.List; import java.io.BufferedReader; import java.io.IOException; import java.io.Inpu
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();
}
HttpClient=newdefaulthttpclient();
HttpPost=新的HttpPost(“http://vogellac2dm.appspot.com/register");
试一试{
List nameValuePairs=新的ArrayList(1);
添加(新的BasicNameValuePair(“注册ID”,“123456789”);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=client.execute(post);
BufferedReader rd=新的BufferedReader(新的InputStreamReader(response.getEntity().getContent());
字符串行=”;
而((line=rd.readLine())!=null){
系统输出打印项次(行);
}
}捕获(IOE异常){
e、 printStackTrace();
}
现在,我有了一个指向.NETWSDL的url
我想调用方法“AddEmployee”,它接受5个字符串参数
因此,从逻辑上讲,我会:
编辑 在没有透露任何详细信息的情况下,我创建了一个我想称之为的示例。下面是一个简单服务的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(看起来是这样的),我建议您使用ApacheAxis之类的工具为您生成客户机类 下载Axis后,您可以运行一个简单的命令来生成客户端类:
java org.apache.axis.wsdl.WSDL2Java wsdlName您使用哪个框架来使用webservices axis或CXF?这很好。我已经将WSDL XML添加到我的OP中。您可以在DefaultHttpClient上提供的任何进一步信息或我应该使用的内容都会有所帮助。谢谢@NullPointerException我不知道这两个是什么。我没有实现任何框架。请使用框架来使用web服务。请看下面的链接,以便我可以下载该工具。。。它将生成一些自定义类,我可以将其放入项目中,并且我不需要在服务器上安装任何工具?@adam您必须将apache axis客户端JAR添加到您的项目中。是否有任何理由我应该选择axis而不是Axis2?我甚至会选择JDK>=1.6中包含的标准java sdk webservice客户端工具。无需将axis之类的东西拖到客户机中(wsimport等包含在JDK中,API包含在标准SE JRE中)。