當前位置:首頁 » 網頁前端 » javawebservice調用
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

javawebservice調用

發布時間: 2022-09-08 01:43:21

① 怎麼調用java的webservice

我常去看的關於asp文章。你可以去看看,上面說的有!
http://www.ladyland.cn/developer/ASP/applications/200511/10259.html
ASP調用WEBSERVICE ----INDEX---- 1. soap請求方式 2. post請求方式 3. SHOWALLNODE函數(關於節點各屬性和數據顯示) --------------------- 一.SOAP請求示例 下面是一個 SOAP 請求示例。所顯示的佔位符需要由實際值替換。 POST /WebService1/UserSignOn.asmx HTTP/1.1 Host: 192.100.100.81 Content-Type: text/xml; charset=utf-8 Content-Length: length SOAPAction: "http://tempuri.org/LoginByAccount" <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <LoginByAccount xmlns="http://tempuri.org/"> <username>string</username> <password>string</password> </LoginByAccount> </soap:Body> </soap:Envelope> 為了與WEBSERVICE交互,需要構造一個與上完全相同的SOAP請求: <% url = "http://192.100.100.81/WebService1/UserSignOn.asmx" SoapRequest="<?xml version="&CHR(34)&"1.0"&CHR(34)&" encoding="&CHR(34)&"utf-8"&CHR(34)&"?>"& _ "<soap:Envelope xmlns:xsi="&CHR(34)&"http://www.w3.org/2001/XMLSchema-instance"&CHR(34)&" "& _ "xmlns:xsd="&CHR(34)&"http://www.w3.org/2001/XMLSchema"&CHR(34)&" "& _ "xmlns:soap="&CHR(34)&"http://schemas.xmlsoap.org/soap/envelope/"&CHR(34)&">"& _ "<soap:Body>"& _ "<LoginByAccount xmlns="&CHR(34)&"http://tempuri.org/"&CHR(34)&">"& _ "<username>"&username&"</username>"& _ "<password>"&password&"</password>"& _ "</LoginByAccount>"& _ "</soap:Body>"& _ "</soap:Envelope>" Set xmlhttp = server.CreateObject("Msxml2.XMLHTTP") xmlhttp.Open "POST",url,false xmlhttp.setRequestHeader "Content-Type", "text/xml;charset=utf-8" xmlhttp.setRequestHeader "HOST","192.100.100.81" xmlhttp.setRequestHeader "Content-Length",LEN(SoapRequest) xmlhttp.setRequestHeader "SOAPAction", "http://tempuri.org/LoginByAccount" 『一定要與WEBSERVICE的命名空間相同,否則服務會拒絕 xmlhttp.Send(SoapRequest) 『這樣就利用XMLHTTP成功發送了與SOAP示例所符的SOAP請求. 『檢測一下是否成功: Response.Write xmlhttp.Status&」 」 Response.Write xmlhttp.StatusText Set xmlhttp = Nothing %> 如果成功會顯示200 ok,不成功會顯示 500 內部伺服器錯誤? Connection: keep-alive . 成功後就可以利用WEBSERVICE的響應,如下: SOAP響應示例 下面是一個 SOAP 響應示例。所顯示的佔位符需要由實際值替換。 HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <LoginByAccountResponse xmlns="http://tempuri.org/"> <LoginByAccountResult>string</LoginByAccountResult> </LoginByAccountResponse> </soap:Body> </soap:Envelope> 這是與剛才SOAP請求示例所對應的SOAP響應示例,在成功發送請求後,就可以查看該響應 : If xmlhttp.Status = 200 Then Set xmlDOC =server.CreateObject("MSXML.DOMDocument") xmlDOC.load(xmlhttp.responseXML) xmlStr = xmlDOC.xml Set xmlDOC=nothing xmlStr = Replace(xmlStr,"<","<") xmlStr = Replace(xmlStr,">",">") Response.write xmlStr Else Response.Write xmlhttp.Status&" " Response.Write xmlhttp.StatusText End if 請求正確則給出完整響應,請求不正確(如賬號,密碼不對)響應的內容就會信息不完整. 取出響應里的數據,如下: If xmlhttp.Status = 200 Then Set xmlDOC = server.CreateObject("MSXML.DOMDocument") xmlDOC.load(xmlhttp.responseXML) Response.Write xmlDOC.documentElement.selectNodes("//LoginByAccountResult")(0).text 『顯示節點為LoginByAccountResult的數據(有編碼則要解碼) Set xmlDOC = nothing Else Response.Write xmlhttp.Status&" " Response.Write xmlhttp.StatusText End if 顯示某節點各個屬性和數據的FUNCTION: Function showallnode(rootname,myxmlDOC)望大家不斷完鄯 2005-1-9 writed by 844 if rootname<>"" then set nodeobj=myxmlDOC.documentElement.selectSingleNode("//"&rootname&"")當前結點對像 nodeAttributelen=myxmlDOC.documentElement.selectSingleNode("//"&rootname&"").attributes.length當前結點屬性數 returnstring=returnstring&"<BR>節點名稱:"&rootname if nodeobj.text<>"" then returnstring=returnstring&"<BR>節點的文本:("&nodeobj.text&")" end if returnstring=returnstring&"<BR>{<BR>" if nodeAttributelen<>0 then returnstring=returnstring&"<BR>屬性數有 "&nodeAttributelen&" 個,分別是:" end if for i=0 to nodeAttributelen-1 returnstring=returnstring&"<li>"&nodeobj.attributes(i).Name&": "&nodeobj.getAttribute(nodeobj.attributes(i).Name)&" </li>" next if nodeobj.childNodes.Length<>0 then if nodeobj.hasChildNodes() and lcase(nodeobj.childNodes.item(0).nodeName)<>"#text" then是否有子節點 set childnodeobj=nodeobj.childNodes childnodelen=nodeobj.childNodes.Length returnstring=returnstring&"<BR><BR>有 "&childnodelen&" 個子節點;<BR>分別是: " for i=0 to childnodelen-1 returnstring=returnstring&"<li>"&childnodeobj.item(i).nodeName&"</li>" next end if end if returnstring=returnstring&"<BR>}<BR>" response.write returnstring set nodeobj=nothing end if End Function 可以這樣用: If xmlhttp.Status = 200 Then Set xmlDOC = server.CreateObject("MSXML.DOMDocument") xmlDOC.load(xmlhttp.responseXML) showallnode "LoginByAccountResponse",xmlDOC』調用SHOWALLNODE Set xmlDOC = nothing Else Response.Write xmlhttp.Status&" " Response.Write xmlhttp.StatusText End if 二.POST請求示例 HTTP POST 下面是一個 HTTP POST 請求示例。所顯示的佔位符需要由實際值替換。 POST /WebService1/UserSignOn.asmx/LoginByAccount HTTP/1.1 Host: 192.100.100.81 Content-Type: application/x-www-form-urlencoded Content-Length: length username=string&password=string 構造POST請求: <% url = "http://192.100.100.81/WebService1/UserSignOn.asmx/LoginByAccount" SoapRequest="username="&username&"&password="&password Set xmlhttp = server.CreateObject("Msxml2.XMLHTTP") xmlhttp.Open "POST",url,false xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"』注意 xmlhttp.setRequestHeader "HOST","192.100.100.81" xmlhttp.setRequestHeader "Content-Length",LEN(SoapRequest) xmlhttp.Send(SoapRequest) 『這樣就利用XMLHTTP成功發送了與HTTP POST示例所符的POST請求. 『檢測一下是否成功: Response.Write xmlhttp.Status&」 」 Response.Write xmlhttp.StatusText Set xmlhttp = Nothing %> 如果成功會顯示200 ok,不成功會顯示 500 內部伺服器錯誤? Connection: keep-alive . 成功後就可以利用WEBSERVICE的響應,如下: HTTP POST 下面是一個 HTTP POST 響應示例。所顯示的佔位符需要由實際值替換。 HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <string xmlns="http://tempuri.org/">string</string> 顯示: If xmlhttp.Status = 200 Then Set xmlDOC = server.CreateObject("MSXML.DOMDocument") xmlDOC.load(xmlhttp.responseXML) showallnode "string",xmlDOC調用SHOWALLNODE Set xmlDOC = nothing Else Response.Write xmlhttp.Status&" " Response.Write xmlhttp.StatusText End if 以上是ASP用XMLHTTP組件發送SOAP請求,調用WEBSERVICE的方法,本人推薦在ASP環境下使用第一種方法,如果有更好的方法請聯系本人mailto:[email protected] .使用HTTP GET的方式如果有中文會出問題,數據量又不大。用HTTP POST的方法感覺多此一舉,其實上面的例子就是用POST的方式,只不過不是用POST的請求。用SOAP TOOLKIT要裝軟體,而且已沒有後繼版本

② java程序怎麼調用webservice介面,實現發送簡訊功能

java程序怎麼調用webservice介面,實現發送簡訊功能
首先我訪問不到你提供的webservice
你可以直接找那個給你webservice的人對他提供的方法進行詢問
不知你是什麼項目框架所以不能給出具體的方案,如果是spring的話直接在配置文件中進行配置就可。首先必須獲得客戶端以及地址,客戶端提供的是介面的定義及規范,地址是要我們進行連接的。

例如:
<bean id="XXService" class="com.xx.客戶端介面" factory-bean="XXServiceFactory" factory-method="create" />
<bean id="XXServiceFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.xx.客戶端介面" />
<property name="address" value="地址/>
</bean>
然後你就可以在你的業務層進行注入,這個是採用cxf的方式,當然也可以有其他方式

③ java 如何實現webservice 怎麼調用介面

一、利用jdkweb服務api實現,這里使用基於SOAPmessage的Web服務

①.首先建立一個WebservicesEndPoint:packageHello;
importjavax.jws.WebService;
importjavax.jws.WebMethod;
importjavax.xml.ws.Endpoint;
@WebService
publicclassHello{
@WebMethod
publicStringhello(Stringname){
return"Hello,"+name+" ";
}
publicstaticvoidmain(String[]args){
//createandpublishanendpoint
Hellohello=newHello();
Endpointendpoint=Endpoint.publish("


,hello);
}
}
②.使用apt編譯Hello.java(例:apt-d[存放編譯後的文件目錄]Hello.java),
會生成jaws目錄
③.使用javaHello.Hello運行,然後將瀏覽器指向


就會出現下列顯示
④.使用wsimport生成客戶端使用如下:
wsimport-p.-keep


這時,會在當前目錄中生成如下文件:
⑤.客戶端程序:
1classHelloClient{
2publicstaticvoidmain(Stringargs[]){
3HelloServiceservice=newHelloService();
4HellohelloProxy=service.getHelloPort();
5Stringhello=helloProxy.hello("你好");
6System.out.println(hello);
7}
8}

以上方法還稍顯繁瑣,還有更加簡單的方法


二、使用xfire,我這里使用的是myeclipse集成的xfire進行測試的利用xfire開發WebService,可以有三種方法:

1. 一種是從javabean中生成;

2.一種是從wsdl文件中生成;

3. 還有一種是自己建立webservice

步驟如下:

用myeclipse建立webservice工程,目錄結構如下:首先建立webservice介面,

代碼如下:

1packagecom.myeclipse.wsExample;
2//GeneratedbyMyEclipse
3
{
5
6publicStringexample(Stringmessage);
7
8}
接著實現這個借口:
1packagecom.myeclipse.wsExample;
2//GeneratedbyMyEclipse
3
{
5
6publicStringexample(Stringmessage){
7returnmessage;
8}
9
10}



修改service.xml文件,加入以下代碼:

1<service>
2<name>HelloWorldService</name>
3<serviceClass>
4com.myeclipse.wsExample.IHelloWorldService
5</serviceClass>
6<implementationClass>
7com.myeclipse.wsExample.HelloWorldServiceImpl
8</implementationClass>
9<style>wrapped</style>
10<use>literal</use>
11<scope>application</scope>
12</service>


把整個項目部署到tomcat伺服器中打開瀏覽器,輸入http://localhost:8989/HelloWorld/services/HelloWorldService?wsdl,可以看到如下:

然後再展開HelloWorldService後面的wsdl可以看到:

客戶端實現如下:

1packagecom.myeclipse.wsExample.client;
2
3importjava.net.MalformedURLException;
4importjava.net.URL;
5
6importorg.codehaus.xfire.XFireFactory;
7importorg.codehaus.xfire.client.Client;
8importorg.codehaus.xfire.client.XFireProxyFactory;
9importorg.codehaus.xfire.service.Service;
10importorg.codehaus.xfire.service.binding.ObjectServiceFactory;
11
12importcom.myeclipse.wsExample.IHelloWorldService;
13
14publicclassHelloWorldClient{
15publicstaticvoidmain(String[]args)throwsMalformedURLException,Exception{
16//TODOAuto-generatedmethodstub
17Services=newObjectServiceFactory().create(IHelloWorldService.class);
18XFireProxyFactoryxf=newXFireProxyFactory(XFireFactory.newInstance().getXFire());
19Stringurl="

20
21try
22{
23
24IHelloWorldServicehs=(IHelloWorldService)xf.create(s,url);
25Stringst=hs.example("zhangjin");
26System.out.print(st);
27}
28catch(Exceptione)
29{
30e.printStackTrace();
31}
32}
33
34}

有時候我們知道一個wsdl地址,比如想用java客戶端引用net做得webservice,使用myeclipse引用,但是卻出現無法通過驗證的錯誤,這時我們可以直接在類中引用,步驟如下:

1.publicstaticvoidmain(String[]args)throwsMalformedURLException,Exception{
2.//TODOAuto-generatedmethodstub



④ 現在java調用webservice是用什麼技術

JAVA調用WS介面現在用的比較多就是AXIS和CXF了
最早的時候是使用AXIS的比較多,因為這個是最早支持JAVA的WS介面的,像ECLIPSE里都自帶了AXIS,然後因為AXIS很久沒有更新了,這時候CXF慢慢進入大家眼中
CXF的介面實現起來更簡單,和其它語言實現的介面互相調用的時候兼容性也很好,再加上還有REST可以更簡單的訪問資源,現在很多新項目都會考慮用CXF,但是有很多老項目還是用的AXIS,如果維護的話也得能看懂,所以還是可以兩個都應該學學,必竟就是實現的方式有些不同而已,原理都是差不多的
關於CXF這個我之前找到一個哥們寫的一些很不錯的筆記,如果有興趣的話你可以去參考參考 http://my.oschina.net/huangyong/blog/294324

⑤ 如何調用別人提供的webservice介面

在項目中選擇【控制台應用程序】,點擊項目右鍵,選擇添加->服務引用。在地址欄中輸入WebServie鏈接地址後回車,點擊確定後在代碼中就可以看到添加的服務應用了,詳細步驟:

1、首先打開VS2013,選擇文件->新建->項目。

⑥ java調用webservice介面具體怎麼調用

Java調用WebService可以直接使用Apache提供的axis.jar自己編寫代碼,或者利用Eclipse自動生成WebService Client代碼,利用其中的Proxy類進行調用。理論上是一樣的,只不過用Eclipse自動生成代碼省事些。
1、編寫代碼方式:
package com.yun.test;
import java.rmi.RemoteException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.message.PrefixedQName;
import org.apache.axis.message.SOAPHeaderElement;
import com.cezanne.golden.user.Exception;
import com.cezanne.golden.user.UserManagerServiceProxy;
import javax.xml.namespace.QName;
import java.net.MalformedURLException;
import javax.xml.rpc.ServiceException;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPException;

public class testWebService {
public static String getResult() throws ServiceException, MalformedURLException, RemoteException, SOAPException
{
//標識Web Service的具體路徑
String endpoint = "WebService服務地址";
// 創建 Service實例
Service service = new Service();
// 通過Service實例創建Call的實例
Call call = (Call) service.createCall();
//將Web Service的服務路徑加入到call實例之中.
call.setTargetEndpointAddress( new java.net.URL(endpoint) );//為Call設置服務的位置
// 由於需要認證,故需要設置調用的SOAP頭信息。
Name headerName = new PrefixedQName( new QName("發布的wsdl里的targetNamespace里的url", "string_itemName") );
org.apache.axis.message.SOAPHeaderElement header = new SOAPHeaderElement(headerName);
header.addTextNode( "blablabla" );
call.addHeader(header);

// SOAPHeaderElement soapHeaderElement = new SOAPHeaderElement("發布的wsdl里的targetNamespace里的url", "SoapHeader");
// soapHeaderElement.setNamespaceURI("發布的wsdl里的targetNamespace里的url");
// try
// {
// soapHeaderElement.addChildElement("string_itemName").setValue("blablabla");
// }
// catch (SOAPException e)
// {
// e.printStackTrace();
// }
// call.addHeader(soapHeaderElement);
//調用Web Service的方法
org.apache.axis.description.OperationDesc oper;
org.apache.axis.description.ParameterDesc param;
oper = new org.apache.axis.description.OperationDesc();
oper.setName("opName");
param = new org.apache.axis.description.ParameterDesc(new javax.xml.namespace.QName("", "arg0"), org.apache.axis.description.ParameterDesc.IN, new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"), java.lang.String.class, false, false);
param.setOmittable(true);
oper.addParameter(param);
param = new org.apache.axis.description.ParameterDesc(new javax.xml.namespace.QName("", "arg1"), org.apache.axis.description.ParameterDesc.IN, new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"), java.lang.String.class, false, false);
param.setOmittable(true);
oper.addParameter(param);
param = new org.apache.axis.description.ParameterDesc(new javax.xml.namespace.QName("", "arg2"), org.apache.axis.description.ParameterDesc.IN, new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"), java.lang.String.class, false, false);
param.setOmittable(true);
oper.addParameter(param);

⑦ 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通過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和postmain調用的區別

區別是WebService可以有Get、Post、Soap、Document四種方式調用。
我們可以把webservice看做是web伺服器上的一個應用,web伺服器是webservice的一個容器。通過wximport生成代碼。通過客戶端編程方式。
通過URLConnection方式調用。

⑩ 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());

}

}

}