『壹』 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;
}
然後解析字元串就好了。是不是很簡單
『貳』 javaweb如何實現請求和響應
先來看一個流程圖:
伺服器處理請求的流程:
(1)伺服器每次收到請求時,都會為這個請求開辟一個新的線程。
(2)伺服器會把客戶端的請求數據封裝到request對象中,request就是請求數據的載體!
(3)伺服器還會創建response對象,這個對象與客戶端連接在一起,它可以用來向客戶端發送響應。
由流程圖可以看出,在JavaWeb的請求與響應中,最重要的兩個參數為request以及response,這兩參數在Servlet的service( )方法中。
1、response概念:
response是Servlet.service方法的一個參數,類型為javax.servlet.http.HttpServletResponse。在客戶端發出每個請求時,伺服器都會創建一個response對象,並傳入給Servlet.service()方法。response對象是用來對客戶端進行響應的,這說明在service()方法中使用response對象可以完成對客戶端的響應工作。
response對象的功能分為以下四種:
(1)設置響應頭信息
(2)發送狀態碼
(3)設置響應正文
(4)重定向
2、response響應正文
response是響應對象,向客戶端輸出響應正文(響應體)可以使用response的響應流,repsonse一共提供了兩個響應流對象:
(1)PrintWriter out = response.getWriter():獲取字元流;
(2)ServletOutputStream out = response.getOutputStream():獲取位元組流;
當然,如果響應正文內容為字元,那麼使用response.getWriter(),如果響應內容是位元組,例如下載時,那麼可以使用response.getOutputStream()。
注意,在一個請求中,不能同時使用這兩個流!也就是說,要麼你使用repsonse.getWriter(),要麼使用response.getOutputStream(),但不能同時使用這兩個流。不然會拋出illegalStateException異常。
『叄』 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 = "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();
}
}
}
3,使用wsdl2java把WSDL文件轉成本地類,然後像本地類一樣使用,即可。
這是像我這種懶人最喜歡的方式,仍然以前面的global weather report為例。
首先 java org.apache.axis.wsdl.WSDL2Java http://www.webservicex.net/globalweather.asmx.WSDL
原本的網址是http://www.webservicex.net/globalweather.asmx?WSDL,中間個各問號,但是Linux下面它不能解析,所以去掉問號,改為點號。
那麼就會出現4個文件:
GlobalWeather.java GlobalWeatherLocator.java GlobalWeatherSoap.java GlobalWeatherSoapStub.java
其中GlobalWeatherSoap.java是我們最為關心的介面文件,如果你對RMI等SOAP實現的具體細節不感興趣,那麼你只需要看介面文件即可,在使用的時候,引入這個介面即可,就好像使用本地類一樣。
『肆』 一個介面是javaweb項目的介面,ios怎麼調用這個介面
在B系統寫一個servlet(用ssh的話寫個action,其實是一樣的),在url上配置參數,B系統接受後進行邏輯處理,再把返回值print出去。
『伍』 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服務的返回值。
『陸』 javaweb、jsp中調用外部的http介面
import java.net.*;
import java.io.*;
public class URLReader {
public static void main(String[] args) throws Exception {
URL yahoo = new URL("http://www..com/query.jsp?param1=value2¶m2=value2");
BufferedReader in = new BufferedReader(
new InputStreamReader(
yahoo.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
『柒』 java 程序向其它程序提供介面如何完成
是指java 和其他語言的數據交互嗎?
1:採用J2EE 的Servlet 完成特定功能,部署Tomcat後 其他語言中可通過Http請求訪問
2:直接採用JSP ,和Servlet類似
3:採用Socket和其他程序通訊
4:採用中間件,比如 和Flex通訊 可以使用LCDS或者Blazeds 直接在Flex中調用Java端的方法..
5:採用WebService 使用web服務 與其他程序交互!
『捌』 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
『拾』 javaweb項目中怎麼調用webservice的介面,並從客戶端判斷輸入的數據,從介面反饋新數據~
您好,很高興回答您的問題,
對於webservice 有2種風格:1:restful , 2:soap
對於第一種 是最直觀的 webservice服務, 可以直接在瀏覽器上通過地址訪問。
對於第二種 使用的是soap協議,在請求頭上 需要添加soap頭,
這二種 風格 都可以使用 httpconnection 進行調用, 只是 對於第二種會稍微麻煩一點。
另外 java 也有專門對於 webservice訪問的包裝, 如:cxf ,axis2 樓主可以對他們進行調查!