java调用webservice接口
⑴ 使用浏览器怎么调用java Web Service的接口
Java通过WSDL文件来调用webservice:
注意,以下的代码并没有经过真正的测试,只是说明这些情况,不同版本的Axis相差很大,大家最好以apache网站上的例子为准,这里仅仅用于说明其基本用法。
1,直接AXIS调用远程的web service
这种方法比较适合那些高手,他们能直接看懂XML格式的WSDL文件,我自己是看不懂的,尤其我不是专门搞这行的,即使一段时间看懂,后来也就忘记了。直接调用模式如下:
import java.util.Date;
import java.text.DateFormat;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
import java.lang.Integer;
import javax.xml.rpc.ParameterMode;
public class caClient {
public static void main(String[] args) {
try {
String endpoint = "";
//直接引用远程的wsdl文件
//以下都是套路
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(endpoint);
call.setOperationName("addUser");//WSDL里面描述的接口名称
call.addParameter("userName", org.apache.axis.encoding.XMLType.XSD_DATE,
javax.xml.rpc.ParameterMode.IN);//接口的参数
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型
String temp = "测试人员";
String result = (String)call.invoke(new Object[]{temp});
//给方法传递参数,并且调用方法
System.out.println("result is "+result);
}
catch (Exception e) {
System.err.println(e.toString());
}
}
}
2,直接SOAP调用远程的webservice
这种模式我从来没有见过,也没有试过,但是网络上有人贴出来,我也转过来
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.rpc.*;
import java.io.*;
import java.net.*;
import java.util.Vector;
public class caService{
public static String getService(String user) {
URL url = null;
try {
url=new URL("");
} catch (MalformedURLException mue) {
return mue.getMessage();
}
// This is the main SOAP object
Call soapCall = new Call();
// Use SOAP encoding
soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
// This is the remote object we're asking for the price
soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized");
// This is the name of the method on the above object
soapCall.setMethodName("getUser");
// We need to send the ISBN number as an input parameter to the method
Vector soapParams = new Vector();
// name, type, value, encoding style
Parameter isbnParam = new Parameter("userName", String.class, user, null);
soapParams.addElement(isbnParam);
soapCall.setParams(soapParams);
try {
// Invoke the remote method on the object
Response soapResponse = soapCall.invoke(url,"");
// Check to see if there is an error, return "N/A"
if (soapResponse.generatedFault()) {
Fault fault = soapResponse.getFault();
String f = fault.getFaultString();
return f;
} else {
// read result
Parameter soapResult = soapResponse.getReturnValue ();
// get a string from the result
return soapResult.getValue().toString();
}
} catch (SOAPException se) {
return se.getMessage();
}
}
}
⑵ JAVA怎样调用https类型的webservice
1.打开webService链接,右键属性—》证书—》详细信息—》复制到文件,保存cer格式的文件。
2. 复制下面的cmd命令,执行keytool命令,生成keystore文件,例如
c:\nciic.keystore
keytool -import -alias nciic -file c:\jswszx.cer -keystore c:\nciic.keystore
它会提示输入密码,随便输入,例如:123456,回车
4.他会提示是否信任这个认证,输入Y,回车,指定目录下就会生成nciic.keystore文件
它会提示输入密码,随便输入,例如:123456,回车
4.他会提示是否信任这个认证,输入Y,回车,指定目录下就会生成nciic.keystore文件
5.修改Java代码
在调用接口方法之前,添加如下代码:
System.setProperty("javax.NET.ssl.trustStore","c://nciic.keystore"); System.setProperty("java.protocol.handler.pkgs","com.sun.Net.ssl.internal.www.protocol");
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
⑶ java如何调用webservice接口
Java通过WSDL文件来调用webservice直接调用模式如下:
import java.util.Date;
import java.text.DateFormat;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
import java.lang.Integer;
import javax.xml.rpc.ParameterMode;
public class caClient {
public static void main(String[] args) {
try {
String endpoint = "http://localhost:8080/ca3/services/caSynrochnized?wsdl";
//直接引用远程的wsdl文件
//以下都是套路
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(endpoint);
call.setOperationName("addUser");//WSDL里面描述的接口名称
call.addParameter("userName", org.apache.axis.encoding.XMLType.XSD_DATE,
javax.xml.rpc.ParameterMode.IN);//接口的参数
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型
String temp = "测试人员";
String result = (String)call.invoke(new Object[]{temp});
//给方法传递参数,并且调用方法
System.out.println("result is "+result);
}
catch (Exception e) {
System.err.println(e.toString());
}
}
}
⑷ Java客户端调用Webservice接口求代码
客户端获得自定义对象包路径必须和服务器端相同,当然在客户端也可以不用建该对象,可以将客户端的自定义对象打成jar包,然后在客户端引用。
猜想用反射也可以实现改对象,但目前没有写相关例子。
<p>importjava.io.Serializable;</p><p>{
/**
*客户端必须有与服务器端相同的自定义对象
*/
=1L;
privateStringid;
privateStringname;
publicStringgetId(){
returnid;
}
publicvoidsetId(Stringid){
this.id=id;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
}</p>
[java]viewplain
packageclient;
importpo.Hello;
{
/*
[java]viewplain
*该方法名必须和服务接口一致
[java]viewplain
*/
[java]viewplain
publicHelloexample();
[java]viewplain
<preclass="java"name="code">packageclient;
importjava.net.MalformedURLException;//importjava.net.URL;
//importorg.codehaus.xfire.client.Client;
importorg.codehaus.xfire.client.XFireProxyFactory;
importorg.codehaus.xfire.service.Service;
importorg.codehaus.xfire.service.binding.ObjectServiceFactory;
importpo.Hello;
publicclassServicesClient{
publicstaticvoidmain(String[]arg)throwsMalformedURLException,
Exception{
Stringxml="http://localhost:8080/web/services/HeloWebService";
=newObjectServiceFactory();
ServiceserviceModel=objectServiceFactory
.create(IClientHelloManager.class);
=newXFireProxyFactory();
IClientHelloManagerservice=(IClientHelloManager)xFireProxyFactory
.create(serviceModel,xml);
HellolHello=service.example();
System.out.println(lHello.getId());
System.out.println(lHello.getName());
//Clientclient=newClient(newURL(
//"http://localhost:8080/web/services/HeloWebService?wsdl"));
//Object[]rsult=client.invoke("example",newObject[]{"hello"});
//Hellohello=(Hello)rsult[0];
//System.out.println();
}
}</pre>
<pre></pre>
<pre></pre>
<pre></pre>
⑸ java程序怎么调用webservice接口,实现发送短信功能
给你一个最简单的方法:
第一、根据http://134.224.102.6:80/CompanySendSmInf/services/SmsInf?wsdl 拿到WSDL文件。
第二、根据Axis的jar包,把WSDL文件生成客服端java代码。(可以把java文件打成jar文件,便于管理。怎么生成java代码,网络里都有说明我就不写了。)
第三、在你工程里用AXIS的功能属性,调用外部接口;给你一个格式模板:
MobileCodeWSLocator l=new MobileCodeWSLocator();//MobileCodeWSLocator是WSDL文件生成客服端java类;
MobileCodeWSSoap s=l.getMobileCodeWSSoap();();//MobileCodeWSSoap 是WSDL文件生成客服端java类
String m=s.getMobileCodeInfo("13811534742", "");
如果你用Axis生成的java类,格式和上面一样;自己参考一下就懂了。
你上面明显的连接异常,第三方服务明显没有开,WEBSERVICE可以设置户名、密码,像行所有的WEBSERVICE都设置,安全考虑吧。
⑹ Java调用webservice接口,一台客户端成功,一台客户端失败
如果可以的话,调用的时候,在服务端上面打断点。这样就知道是真的超时,还是没连接上。不能打断点,就在A上面,先测试一下IP端口是不是通的。
⑺ java语言使用post方式调用webService方式
WebService可以有Get、Post、Soap、Document四种方式调用,以下Java通过post方式调用WebService代码:
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.io.OutputStream;
importjava.io.OutputStreamWriter;
importjava.net.URL;
importjava.net.URLConnection;
importjava.net.URLEncoder;
importorg.apache.cxf.endpoint.Client;
importorg.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
/**
*功能描述:WebService调用
*
*/
publicclassClientTest{
/**
*功能描述:HTTP-POST
*
*/
publicStringpost(){
OutputStreamWriterout=null;
StringBuildersTotalString=newStringBuilder();
try{
URLurlTemp=newURL(
"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity");
URLConnectionconnection=urlTemp.openConnection();
connection.setDoOutput(true);
out=newOutputStreamWriter(connection.getOutputStream(),"UTF-8");
StringBuffersb=newStringBuffer();
sb.append("byProvinceName=福建");
out.write(sb.toString());
out.flush();
StringsCurrentLine;
sCurrentLine="";
InputStreaml_urlStream;
l_urlStream=connection.getInputStream();//请求
BufferedReaderl_reader=newBufferedReader(newInputStreamReader(
l_urlStream));
while((sCurrentLine=l_reader.readLine())!=null){
sTotalString.append(sCurrentLine);
}
}catch(Exceptione){
e.printStackTrace();
}finally{
if(null!=out){
try{
out.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
returnsTotalString.toString();
}
}