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