国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
使用Java客戶端類(lèi)調(diào)用c# WebService和xml rpc server
使用Java客戶端類(lèi)調(diào)用c# WebService和xml rpc server
2008年01月14日 星期一 13:16
 本文介紹一個(gè)非常實(shí)用的Java客戶端工具類(lèi)來(lái)調(diào)用C# WebServices和apache xml rpc server,這個(gè)類(lèi)的源碼是從網(wǎng)上下載的,我在博客網(wǎng)做項(xiàng)目的時(shí)候一直使用這個(gè)類(lèi)來(lái)調(diào)試C# WebServices和MetaWeblog API。順便在這里也給大家介紹一下C#如何處理此類(lèi)發(fā)送的xml數(shù)據(jù)。
使用這個(gè)類(lèi)不用安裝任何第三方工具,因?yàn)椴捎胔ttp的方式發(fā)送xml文件,所以你只需要安裝好JDK就可以了。執(zhí)行此類(lèi)還可以獲得WebServices或xml rpc server返回的xml字符流,你可以根據(jù)返回的xml數(shù)據(jù)來(lái)進(jìn)行其他程序處理。通過(guò)這種方式實(shí)現(xiàn)了Java平臺(tái)和.NET平臺(tái)的數(shù)據(jù)交換和WebService調(diào)用。
下面是此類(lèi)的源代碼SOAPClient4XG.java:

/**
* SOAPClient4XG. Read the SOAP envelope file passed as the second
* parameter, pass it to the SOAP endpoint passed as the first parameter, and
* print out the SOAP envelope passed as a response. with help from Michael
* Brennan 03/09/01
*
*
* @author Bob DuCharme
* @version 1.1
* @param SOAPUrl URL of SOAP Endpoint to send request.
* @param xmlFile2Send A file with an XML document of the request.
*
* 5/23/01 revision: SOAPAction added
*/

import java.io.*;
import java.net.*;

public class SOAPClient4XG {
public static void main(String[] args) throws Exception {

if (args.length < 2) { //小于
System.err.println("Usage: java SOAPClient4XG " +
"http://soapURL soapEnvelopefile.xml" +
" [SOAPAction]");
System.err.println("SOAPAction is optional.");
System.exit(1);
}

String SOAPUrl = args[0];
String xmlFile2Send = args[1];

String SOAPAction = "";
if (args.length > 2) //大于
SOAPAction = args[2];

// Create the connection where we‘re going to send the file.
URL url = new URL(SOAPUrl);
URLConnection connection = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) connection;

// Open the input file. After we copy it to a byte array, we can see
// how big it is so that we can set the HTTP Cotent-Length
// property. (See complete e-mail below for more on this.)

FileInputStream fin = new FileInputStream(xmlFile2Send);

ByteArrayOutputStream bout = new ByteArrayOutputStream();

// Copy the SOAP file to the open connection.
copy(fin,bout);
fin.close();

byte[] b = bout.toByteArray();

// Set the appropriate HTTP parameters.
httpConn.setRequestProperty( "Content-Length",
String.valueOf( b.length ) );
httpConn.setRequestProperty("Content-Type","text/xml; charset=utf-8");
httpConn.setRequestProperty("SOAPAction",SOAPAction);
httpConn.setRequestMethod( "POST" );
httpConn.setDoOutput(true);
httpConn.setDoInput(true);

// Everything‘s set up; send the XML that was read in to b.
OutputStream out = httpConn.getOutputStream();
out.write( b );
out.close();

// Read the response and write it to standard out.

InputStreamReader isr =
new InputStreamReader(httpConn.getInputStream());
BufferedReader in = new BufferedReader(isr);

String inputLine;

while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);

in.close();
}

// copy method from From E.R. Harold‘s book "Java I/O"
public static void copy(InputStream in, OutputStream out)
throws IOException {

// do not allow other threads to read from the
// input or write to the output while copying is
// taking place

synchronized (in) {
synchronized (out) {

byte[] buffer = new byte[256];
while (true) {
int bytesRead = in.read(buffer);
if (bytesRead == -1) break;
out.write(buffer, 0, bytesRead);
}
}
}
}
}

編譯:javac SOAPClient4XG.java
運(yùn)行的命令格式: java -classpath . SOAPClient4XG http://localhost/BokeServices/Service1.asmx c:loginReq.xml http://tempuri.org/UserLoginReq,不過(guò)先不要運(yùn)行上面的命令,先介紹一下命令行的意思,http://localhost/BokeServices/Service1.asmx是C# WebService的地址,c:loginReq..xml里的內(nèi)容是調(diào)用的WebService方法的xml文件, http://tempuri.org是WebService方法的命名空間,一定要有,否則調(diào)用失敗,如果你在C# WebServices中使用了方法默認(rèn)的命名空間的話,就使用http://tempuri.org,否則要與C#中定義的一致,UserLoginReq是C# WebServices的方法名。注意xml文件中的方法名和參數(shù)名是與C# WebServices的方法名、參數(shù)名是一一對(duì)應(yīng)的(參數(shù)順序是可以顛倒的)。
我先介紹一個(gè)簡(jiǎn)單的例子(c:loginReq.xml),這個(gè)xml文件調(diào)用了遠(yuǎn)程C# WebService的UserLoginReq方法,并帶UserAcc(用戶名)和UserPwd(口令)兩個(gè)參數(shù),調(diào)用成功后C#會(huì)自動(dòng)返回一個(gè)xml格式的SOAP包。
<?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>
<UserLoginReq xmlns="http://tempuri.org/">
<UserAcc>baozheng</UserAcc>
<UserPwd>mypwd</UserPwd>

</UserLoginReq>
</soap:Body>
</soap:Envelope>

現(xiàn)在看一下C# WebServices的UserLoginReq的方法的定義:
public struct UserLoginResp
{
public string UserAcc;
public int Result;
}


[WebMethod]


public UserLoginResp UserLoginReq(string UserAcc,string UserPwd,int ReqFrom)
{

}

注意結(jié)構(gòu)UserLoginResp,C# WebServices返回SOAP信息時(shí)會(huì)自動(dòng)將UserLoginResp結(jié)構(gòu)轉(zhuǎn)換成xml的格式。

用此類(lèi)做xml rpc server 的客戶端也很簡(jiǎn)單,下文是一個(gè)客戶端rpc.xml文件,調(diào)用了xml rpc server 端實(shí)現(xiàn)的metaWeblog.deletePost方法。
<?xml version="1.0" encoding="utf-8"?>
<methodCall>
<methodName>metaWeblog.deletePost</methodName>
<params>
<param><value>appKeyValue</value></param>
<param><value>746</value></param>

<param><value>baozheng</value></param>
<param><value>Hello123</value></param>
</params>
</methodCall>

調(diào)用命令的格式:
java -classpath %CLASSPATH%;. SOAPClient4XG. http://192.168.25.97:8080/BokeeXmlRpc/xml-rpc rpc.xml

對(duì)比調(diào)用WebServices的命令行,可以看出調(diào)用xml rpc server的命令行少一個(gè)方法名參數(shù)。http://192.168.25.97:8080/BokeeXmlRpc/xml-rpc 是提供xml rpc 調(diào)用的server端servlet地址,關(guān)于如何用apache xml rpc技術(shù)實(shí)現(xiàn)MetalogAPI的帖子將在近期整理發(fā)布。

注:上文的左右尖括號(hào)為保證正常發(fā)貼替換為全角。
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
WebService學(xué)習(xí),開(kāi)發(fā)總結(jié)--唐木之無(wú)線觀察
WebService CXF學(xué)習(xí)(進(jìn)階篇2):JAX-WS講解
J2EE vs .NET--建置XML WebServices
藍(lán)色理想 - WebService的基本概念
JAVA6開(kāi)發(fā)WebService (一)
Java開(kāi)發(fā)中經(jīng)常使用到的幾種WebService技術(shù)實(shí)現(xiàn)方案
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服