⑴ java程序怎么调用webservice接口,实现发送短信功能
给你一个最简单的方法:
第一、根据 拿到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实现调用webserver
一、利用jdk web服务api实现,这里使用基于 SOAP message 的 Web 服务
1.首先建立一个Web services EndPoint:
Java代码
package Hello;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.xml.ws.Endpoint;
@WebService
public class Hello {
@WebMethod
public String hello(String name) {
return "Hello, " + name + "\n";
}
public static void main(String[] args) {
// create and publish an endpoint
Hello hello = new Hello();
Endpoint endpoint = Endpoint.publish("http://localhost:8080/hello", hello);
}
}
Java代码
package Hello;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.xml.ws.Endpoint;
@WebService
public class Hello {
@WebMethod
public String hello(String name) {
return "Hello, " + name + "\n";
}
public static void main(String[] args) {
// create and publish an endpoint
Hello hello = new Hello();
Endpoint endpoint = Endpoint.publish("http://localhost:8080/hello", hello);
}
}
2.使用 apt 编译 Hello.java(例:apt -d [存放编译后的文件目录] Hello.java ) ,会生成 jaws目录
3.使用java Hello.Hello运行,然后将浏览器指向http://localhost:8080/hello?wsdl就会出现下列显示
4.使用wsimport 生成客户端
使用如下:wsimport -p . -keep http://localhost:8080/hello?wsdl
5.客户端程序:
Java代码
class HelloClient{
public static void main(String args[]) {
HelloService service = new HelloService();
Hello helloProxy = service.getHelloPort();
String hello = helloProxy.hello("你好");
System.out.println(hello);
}
}
Java代码
class HelloClient{
public static void main(String args[]) {
HelloService service = new HelloService();
Hello helloProxy = service.getHelloPort();
String hello = helloProxy.hello("你好");
System.out.println(hello);
}
}
⑶ java 怎样调用.net 写的webservice
一. 使用axis1.x调用webservice方法
Axis的最常用版本:1.4和2.0版本。以下为1.4版本
核心代码:
// webserviceURL
service_url = "http://vip.cxcod.com/PodApi/GetPodStr.asmx?wsdl";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(service_url));
// 设置要调用的方法
// http://intelink.net/是wsdl中definitions根节点的targetNamespace属性值
call.setOperationName(new QName("http://intelink.net/","GetStrByJobno"));
// 该方法需要的参数
call.addParameter("CustNo",
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.addParameter("passwd",
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.addParameter("Jobno",
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
// 方法的返回值类型
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
// call.setUseSOAPAction(true); //call.setSOAPActionURI("http://intelink.net/GetStrByJobno");
// 调用该方法, new Object[] { CustNo, passwd, Jobno}为参数列表
String xmlStr = call.invoke(new Object[] { CustNo, passwd, Jobno}).toString();
} catch (Exception e) {
e.printStackTrace();
}
JAVA用这种方式调用webservice,需要注意的地方:
1. 服务器未能识别 HTTP 标头 SOAPAction 的值:
症状一:
Web Service + ASP.NET 应用程序部署到服务器默认目录中,在IE中用http://<服务器地址>/<程序目录名>/<默认启动页面名>发生“服务器未能识别 HTTP 标头 SOAPAction 的值”错误。
症状二:
在Java平台上调用.NET Web Service的服务时,出现"服务器未能识别 HTTP 标头 SOAPAction 的值"。
症状三:
在Java平台下调用.NET WEB Service,出现数据时有时无。
解决对策:
给.NET的WebService类(即.asmx文件下的类)添加属性[SoapDocumentService(RoutingStyle=SoapServiceRoutingStyle.RequestElement)]
小知识:
什么是SoapAction?它在WSDL中有何作用?
SOAPAction HTTP request header被用来标识SOAP HTTP请求的目的地,其值是个URI地址。SOAP发送并不限制格式、URI特征或其必须可解析,那么在这种情况下,发送一个HTTP SOAP请求时,其HTTP客户端必须使用/指明SOAPAction HTTP request header。
SOAPAction header的内容可以被用在服务端,诸如:防火墙适当的过滤基于HTTP的SOAP请求消息等场景。SOAPAction header的值为空串("")表示SOAP消息的目的地由HTTP请求的URI标识;无值则表示没有指定这条消息的目的地。
本人补充:
在.NET环境调用.NET WebService出现 “SOAPAction 值在 XML Web services 的所有方法中不唯一的错误”,也可以通过此法解决。
2. 为了Java能够调用WebService的方法,所以。NETP写的WebServiced的每个方法都要声明为Rpc方法,即添加"[SoapRpcMethod.....]".
例如:[WebMethod]
[SoapRpcMethod(Use=SoapBindingUse.Literal,Action= http://tempuri.org/HelloWorld", RequestNamespace = "http://tempuri.org/", ResponseNamespace = "http://tempuri.org/")]
3. 对返回值、参数的处理上:
应尽量将webservice方法的返回值、参数都写成字符串(String)不要使用复杂对象类型,这样便于在网络上传输。避免了复杂对象类型的不易转换问题。。。对于返回类型是字符串数组型的,可以设置返回类型为org.apache.axis.encoding.XMLType.SOAP_VECTOR或java.lang.String[].class.
二.利用xfire调用WebService
XFire是新一代的Java Web服务引擎,XFire使得在JavaEE应用中发布Web服务变得轻而易举。和其他Web服务引擎相比,XFire的配置非常简单,可以非常容易地和Spring集成,它使得Java开发人员终于可以获得和.Net开发人员一样的开发效率。
核心代码:
Service service = new ObjectServiceFactory().create(IWebservice.class);
XFireProxyFactory factory =
new XFireProxyFactory(XFireFactory.newInstance().getXFire());
String url= "http://localhost:8080/webservices/services/webservices";
IWebservice iw = (IWebservice) factory.create(service, url);
List list=iw.getTest();
出处:http://liyuandong.iteye.com/blog/567836
⑷ 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
1.使用HttpClient
用到的jar文件:commons-httpclient-3.1.jar
方法:
预先定义好Soap请求数据,可以借助于XMLSpy Professional软件来做这一步生成。
String soapRequestData = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">" +
"<soap12:Body>" +
" <getCountryCityByIp xmlns=\"http://WebXml.com.cn/\">" +
" <theIpAddress>219.137.167.157</theIpAddress>" +
" </getCountryCityByIp>" +
" </soap12:Body>" +
"</soap12:Envelope>";
然后定义一个PostMethod,这时需要指定web服务的Url;
PostMethod postMethod = new PostMethod(“http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx”);
然后把Soap请求数据添加到PostMethod中
byte[] b = soapRequestData.getBytes("utf-8");
InputStream is = new ByteArrayInputStream(b,0,b.length);
RequestEntity re = new InputStreamRequestEntity(is,b.length,"application/soap+xml; charset=utf-8");
postMethod.setRequestEntity(re);
最后生成一个HttpClient对象,并发出postMethod请求
HttpClient httpClient = new HttpClient();
statusCode = httpClient.executeMethod(postMethod);
String soapRequestData = postMethod.getResponseBodyAsString();
soapRequestData就是调用web服务的Soap响应数据,是xml格式的,可以通过解析soapRequestData来获得调用web服务的返回值。
2.使用Xfire
用到的jar文件xfire-all-1.2.4.jar, jdom-1.0.jar
方法:
定义一个Client对象,指定web服务的wsdl的地址
Client c = new Client(new URL(“http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl”));
调用Client对象的invoke方法,指定web服务的方法名,和参数,返回值是一个Object型的数组。
下面代码调用getVersionTime方法,这个方法没有参数用所以后一个参数使用new Object[0]。
Object[] results = c.invoke(“getVersionTime”, new Object[0]);
3.使用axis2
下载axis2-1.4
方法:
打开控制台,进入axis2-1.4/bin目录
wsdl2java.bat -uri http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl -p ws.clinet.axis2
上述命令执行完后,会在当前目录下生成一个src目录,在src\ ws\ clinet\ axis2目录里生成XXXXCallbackHandler.java和XXXXStub.java两个文件。
wsdl2java 会根据wsdl文件生成web服务的调用接口,参数类,返回值的类。
在调用webservice的时候直接实例化一个XXXXStub的对象,然后调用web服务的方法就可以了。
4. 总结
针对某种工具搭建的Web Service服务可能有与其对应的更简单的调用方法,在这里没有做描述,上述的调用web服务的方法是通用的。
上述三种方法中使用httpclient应该是比较灵活,但是开发效率低,难度大,使用Xfire和axis2比较容易,开发速度快,但是axis2通用性不好,有的web服务用axis2不好用。httpclient和Xfire通用性比较好,鉴于以上特点推荐使用Xfire。
⑹ 怎么用Java通过wsdl地址调用WebService求代码
我觉得这种方法比较适合那些高手,他们能直接看懂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 = "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());
}
}
}
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("http://192.168.0.100:8080/ca3/services/caSynrochnized");
} 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客户端调用Webservice接口流程
给你看看以前写的获取电话号码归属地的代码的三种方法,然后你就懂了。
importjava.io.ByteArrayOutputStream;
importjava.io.FileInputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.net.HttpURLConnection;
importjava.net.URL;
importorg.apache.commons.httpclient.HttpClient;
importorg.apache.commons.httpclient.HttpException;
importorg.apache.commons.httpclient.methods.PostMethod;
publicclassMobileCodeService{
publicvoidhttpGet(Stringmobile,StringuserID)throwsException
{
//http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=string&userID=string
URLurl=newURL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode="+mobile+"&userID="+userID);
HttpURLConnectionconn=(HttpURLConnection)url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode()==HttpURLConnection.HTTP_OK)//200
{
InputStreamis=conn.getInputStream();
=newByteArrayOutputStream();//
byte[]buf=newbyte[1024];
intlen=-1;
while((len=is.read(buf))!=-1)
{
//获取结果
arrayOutputStream.write(buf,0,len);
}
System.out.println("Get方式获取的数据是:"+arrayOutputStream.toString());
arrayOutputStream.close();
is.close();
}
}
publicvoidhttpPost(Stringmobile,StringuserID)throwsHttpException,IOException
{
//访问路径http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo
//HttpClient访问
HttpClienthttpClient=newHttpClient();
PostMethodpm=newPostMethod("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo");
pm.setParameter("mobileCode",mobile);
pm.setParameter("userID",userID);
intcode=httpClient.executeMethod(pm);
System.out.println("状态码:"+code);
//获取结果
Stringresult=pm.getResponseBodyAsString();
System.out.println("获取到的数据是:"+result);
}
publicvoidSOAP()throwsException
{
HttpClientclient=newHttpClient();
PostMethodmethod=newPostMethod("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx");
//设置访问方法的参数
method.setRequestBody(newFileInputStream("C:\soap.xml"));
method.setRequestHeader("Content-Type","text/xml;charset=utf-8");
intcode=client.executeMethod(method);
System.out.println("状态码:"+code);
//获取结果
Stringresult=method.getResponseBodyAsString();
System.out.println("获取到的数据是:"+result);
}
publicstaticvoidmain(String[]args)throwsException{
MobileCodeServicemcs=newMobileCodeService();
mcs.httpGet("18524012513","");
//mcs.httpPost("18524012513","");
//mcs.SOAP();
}
}