當前位置:首頁 » 編程語言 » webservicejava客戶端

webservicejava客戶端

發布時間: 2023-08-16 08:10:41

1. 關於Webservice介面的java客戶端調用

String endpoint="http://localhost:8080/xxx/services/userservice?wsdl";
String id = "11111";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new URL(endpoint));
call.setOperationName("webservice方法名");
String res = (String) call.invoke(new Object[] {id});
看了你的描述覺得你把webservice想得太復雜化了,其實就是一個jar包和幾個類。
以上就是最簡單的webservice客戶端用法,和反射有點像。當然返回值不一定是String,返回的類型和格式要問服務提供方。
我用的是axis的,我不了解websphere什麼的,但是webservice就是那麼易用的東西。

2. Java調用webservice介面,一台客戶端成功,一台客戶端失敗

如果可以的話,調用的時候,在服務端上面打斷點。這樣就知道是真的超時,還是沒連接上。不能打斷點,就在A上面,先測試一下IP埠是不是通的。

3. 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();
}
}

4. 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>

5. webservice中怎樣用java寫客戶端程序()

基於AXIS的web service:

1 比如要建一個Server.java類的web service
public class Server {
public String printInfo(String name){
return "Hello,"+name;
}
}
2 把Server.java改為Server.Jws放到
…\Tomcat 5.5\webapps\axis中,重啟伺服器

3 訪問http://localhost:8080/axis/Server.jws?wsdl

4 在cmd中輸入 cd D:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\axis\WEB-INF
輸入命令:Java -Djava.ext.dirs=lib org.apache.axis.wsdl.WSDL2Java http://localhost:8080/axis/Server.jws?wsdl

5 找到…\Tomcat 5.5\webapps\axis\WEB-INF下生成的localhost文件夾復制到工程中

6 建一個Client端的類進行測試:
public class Client {
public static void main(String arg[]) throws ServiceException, RemoteException{
ServerService ss=new ServerServiceLocator();
Server s=ss.getServer();
System.out.println("............"+s.printInfo("shiyou"));
}
}

6. java客戶端調用webservice 超時問題

用多線程來處理類似問題
將調用這個WebService的程序放到一個獨立線程A中,再創建另一個線程B用來計時,線程A和線程B共享一個變數responseOK。

在線程A中調用WebService之前啟動線程B,成功返回後設定responseOK=true。
線程B啟動後計時,如果responseOK==true則停止計時,如果計時超過20秒,則終止線程A並返回錯誤信息。

似乎webService調用的時候如果服務端超時應該會有異常觸發的,截獲此異常即可。

7. java開發webservice客戶端時,使用axis需要哪些jar包

http://axis.apache.org/axis2/java/core/download.cgi

8. 用java怎麼寫webservice

Web Services以XML作為數據交換的標准格式,它是跨平台的應用,允許以任何方式創建Web Services,在.NET、Java平台上訪問。
在Java平台創建和訪問Web Service多通過Axis完成。Axis本質上就是一個SOAP引擎,提供創建伺服器端、客戶端和網關SOAP操作的基本框架。Axis目前版本是為Java編寫的。在使用Axis訪問Web Service時,需要引入以下包(10個):axis-ant.jar、axis.jar、commons-discovery-0.2.jar、commons-logging-1.0.4.jar、jaxrpc.jar、log4j-1.2.8.jar、saaj.jar、wsdl4j-1.5.1.jar、activation-1.1.jar和mail-1.4.jar。
(1)訪問Java創建的Web Service
在當前Java客戶端應用中添加相應的10個Axis包,編寫客戶端程序:
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

public class Test {
public static void main(String[] args) throws Exception {
try{
String endpoint = "http://localhost:8080/MyService/services/Hello";
Service service = new Service();
Call call = (Call)service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperationName("getHello");
String res = (String) call.invoke(new Object[]{});
System.out.println(res);
}
catch (Exception ex){
ex.printStackTrace();
}
}
}
其中兩處代碼加粗,第一處表示引用Java Web Service的URL,第二處表示公共的方法名稱。

9. java webservice客戶端代碼不生成可以嗎

WebService一般是做介面的,如果你要進行客戶端的調用,則需要生成客戶端代碼。客戶端代碼可以通過工具生成的,比如axis和axis2等都可以,如果你不願意自動生成則需要手動編寫了。

10. java webservice生成的客戶端代碼 需要哪些jar包

1、安裝環境 J2SE SDK 1.4,Tomcat 5.0,eclipse 3.2。
2、下載Axis安裝包。
3、將Axis相關包文件放在WEB-INF\lib目錄下。
4、Axis可選的包:activation.jar; mail.jar; xmlsec-1.4.Beta1.jar拷貝到WEB-INF目錄下,生成客戶端時候需要使用。
Axis支持三種web service的部署和開發,分別為:
1、Dynamic Invocation Interface (DII)
2、Dynamic Proxy方式
3、Stubs方式
建議我們使用Stubs方式,因此我就主要就介紹一下第三種方式。
注意,Java源代碼是放在D:\workspace\test\目錄下,Axis相關包文件放在D:\workspace\test\WEB-INF目錄下。

熱點內容
資料庫的演算法 發布:2025-02-05 20:25:32 瀏覽:859
微信解壓異常 發布:2025-02-05 20:24:39 瀏覽:493
linux0位元組文件夾 發布:2025-02-05 20:23:07 瀏覽:652
專題的腳本怎麼寫 發布:2025-02-05 20:19:18 瀏覽:923
獨立站買什麼伺服器 發布:2025-02-05 20:13:24 瀏覽:296
android鬧鍾設置 發布:2025-02-05 20:12:29 瀏覽:955
計算機代碼經典編程 發布:2025-02-05 19:25:09 瀏覽:757
安卓抖音怎麼換不了白色背景 發布:2025-02-05 19:11:16 瀏覽:810
安卓手機如何變成手寫 發布:2025-02-05 19:11:14 瀏覽:981
esp32搭建自己的伺服器 發布:2025-02-05 18:58:00 瀏覽:318