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

javaweb介面調用介面

發布時間: 2022-11-25 20:46:21

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

使用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服務的返回值。

❷ 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調用 webservice 介面怎麼調用

太簡單了,這個跟Java訪問url是一樣的:

	/**
*程序中訪問http數據介面
*@paramurlStrwebService地址地址
*/
(StringurlStr){
/**網路的url地址*/
URLurl=null;
/**http連接*/
HttpURLConnectionhttpConn=null;
/**//**輸入流*/
BufferedReaderin=null;
StringBuffersb=newStringBuffer();
try{
url=newURL(urlStr);
in=newBufferedReader(newInputStreamReader(url.openStream(),"UTF-8"));
Stringstr=null;
while((str=in.readLine())!=null){
sb.append(str);
}
}catch(Exceptionex){
ex.printStackTrace();
}finally{
try{
if(in!=null){
in.close();
}
}catch(IOExceptionex){
ex.printStackTrace();
}
}
Stringresult=sb.toString();
System.out.println(result);
returnresult;
}

然後解析字元串就好了。是不是很簡單

❹ 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怎麼調用別人給的介面

1、調用WebService,對方給出WebService地址,可以用Axis生成對WebService的調用代碼進行調用
2、對方提供介面文檔和傳輸方式,根據介面文檔調用。
Java介面是一系列方法的聲明,是一些方法特徵的集合,一個介面只有方法的特徵沒有方法的實現,因此這些方法可以在不同的地方被不同的類實現,而這些實現可以具有不同的行為(功能)。
兩種含義:一,Java介面,Java語言中存在的結構,有特定的語法和結構;二,一個類所具有的方法的特徵集合,是一種邏輯上的抽象。前者叫做「Java介面」,後者叫做「介面」。

❻ Java web開發,寫介面功能測試頁面時如何調用欲測程序里的介面

寫一個實現userLogin介面的方法;然後把你頁面傳入的參數放入到該方法裡面,然後把你調用方法返回的結果保存後返回到頁面。

❼ java怎麼調用webservice介面

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();
}
}
}
3,使用wsdl2java把WSDL文件轉成本地類,然後像本地類一樣使用,即可。
這是像我這種懶人最喜歡的方式,仍然以前面的global weather report為例。
首先 java org.apache.axis.wsdl.WSDL2Java
原本的網址是,中間個各問號,但是Linux下面它不能解析,所以去掉問號,改為點號。
那麼就會出現4個文件:
GlobalWeather.java GlobalWeatherLocator.java GlobalWeatherSoap.java GlobalWeatherSoapStub.java
其中GlobalWeatherSoap.java是我們最為關心的介面文件,如果你對RMI等SOAP實現的具體細節不感興趣,那麼你只需要看介面文件即可,在使用的時候,引入這個介面即可,就好像使用本地類一樣。

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

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

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

❾ java如何調用介面方式

如果是已經有了URL的介面
URL url = new URL(介面);
創建鏈接對方介面對象
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

設置請求屬性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");

設置是否向httpUrlConnection輸出,設置是否從httpUrlConnection讀入

conn.setDoOutput(true);
conn.setDoInput(true);
最後斷開鏈接,保證速度
conn.disconnect();
基本就這樣用需要更多的就要自己看api了

❿ 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