用Java编写的简单SOAP客户机调用.Net开发的Web Service
Web Service也不是什么新技术了,尽管了解的时间很长,但一直没用过。现在的一个项目可能要用.Net开发WebService而客户端可能是Java应用,也可能是.Net应用。由于时间要求紧,所以第一位的技术要求就是简单,同时也要具有一定的灵活性。用.Net开发WebService非常简单,就不说了,但怎么用Java调用最容易呢?
在网上搜了一下,找到了一篇文章,原文见http://tech.ccidnet.com/art/295/20030120/37087_1.html
源代码贴在这,供以后参考:
在网上搜了一下,找到了一篇文章,原文见http://tech.ccidnet.com/art/295/20030120/37087_1.html
源代码贴在这,供以后参考:
1import java.io.*;
2import java.net.*;
3public class SOAPClient4XG {
4 public static void main(String[] args) throws Exception {
5 if (args.length < 2) {
6 System.err.println("Usage: java SOAPClient4XG " +
7 "http://soapURL soapEnvelopefile.xml" +
8 " [SOAPAction]");
9 System.err.println("SOAPAction is optional.");
10 System.exit(1);
11 }
12 String SOAPUrl = args[0];
13 String xmlFile2Send = args[1];
14 String SOAPAction = "";
15 if (args.length > 2)
16 SOAPAction = args[2];
17 // Create the connection where we're going to send the file.
18 URL url = new URL(SOAPUrl);
19 URLConnection connection = url.openConnection();
20 HttpURLConnection httpConn = (HttpURLConnection) connection;
21 // Open the input file. After we copy it to a byte array, we can see
22 // how big it is so that we can set the HTTP Cotent-Length
23 // property. (See complete e-mail below for more on this.)
24 FileInputStream fin = new FileInputStream(xmlFile2Send);
25 ByteArrayOutputStream bout = new ByteArrayOutputStream();
26 // Copy the SOAP file to the open connection.
27 copy(fin,bout);
28 fin.close();
29 byte[] b = bout.toByteArray();
30 // Set the appropriate HTTP parameters.
31 httpConn.setRequestProperty( "Content-Length",
32 String.valueOf( b.length ) );
33 httpConn.setRequestProperty("Content-Type","text/xml; charset=utf-8");
34 httpConn.setRequestProperty("SOAPAction",SOAPAction);
35 httpConn.setRequestMethod( "POST" );
36 httpConn.setDoOutput(true);
37 httpConn.setDoInput(true);
38 // Everything's set up; send the XML that was read in to b.
39 OutputStream out = httpConn.getOutputStream();
40 out.write( b );
41 out.close();
42 // Read the response and write it to standard out.
43 InputStreamReader isr =
44 new InputStreamReader(httpConn.getInputStream());
45 BufferedReader in = new BufferedReader(isr);
46 String inputLine;
47 while ((inputLine = in.readLine()) != null)
48 System.out.println(inputLine);
49 in.close();
50 }
51 // copy method from From E.R. Harold's book "Java I/O"
52 public static void copy(InputStream in, OutputStream out)
53 throws IOException {
54 // do not allow other threads to read from the
55 // input or write to the output while copying is
56 // taking place
57 synchronized (in) {
58 synchronized (out) {
59 byte[] buffer = new byte[256];
60 while (true) {
61 int bytesRead = in.read(buffer);
62 if (bytesRead == -1) break;
63 out.write(buffer, 0, bytesRead);
64 }
65 }
66 }
67 }
68}
2import java.net.*;
3public class SOAPClient4XG {
4 public static void main(String[] args) throws Exception {
5 if (args.length < 2) {
6 System.err.println("Usage: java SOAPClient4XG " +
7 "http://soapURL soapEnvelopefile.xml" +
8 " [SOAPAction]");
9 System.err.println("SOAPAction is optional.");
10 System.exit(1);
11 }
12 String SOAPUrl = args[0];
13 String xmlFile2Send = args[1];
14 String SOAPAction = "";
15 if (args.length > 2)
16 SOAPAction = args[2];
17 // Create the connection where we're going to send the file.
18 URL url = new URL(SOAPUrl);
19 URLConnection connection = url.openConnection();
20 HttpURLConnection httpConn = (HttpURLConnection) connection;
21 // Open the input file. After we copy it to a byte array, we can see
22 // how big it is so that we can set the HTTP Cotent-Length
23 // property. (See complete e-mail below for more on this.)
24 FileInputStream fin = new FileInputStream(xmlFile2Send);
25 ByteArrayOutputStream bout = new ByteArrayOutputStream();
26 // Copy the SOAP file to the open connection.
27 copy(fin,bout);
28 fin.close();
29 byte[] b = bout.toByteArray();
30 // Set the appropriate HTTP parameters.
31 httpConn.setRequestProperty( "Content-Length",
32 String.valueOf( b.length ) );
33 httpConn.setRequestProperty("Content-Type","text/xml; charset=utf-8");
34 httpConn.setRequestProperty("SOAPAction",SOAPAction);
35 httpConn.setRequestMethod( "POST" );
36 httpConn.setDoOutput(true);
37 httpConn.setDoInput(true);
38 // Everything's set up; send the XML that was read in to b.
39 OutputStream out = httpConn.getOutputStream();
40 out.write( b );
41 out.close();
42 // Read the response and write it to standard out.
43 InputStreamReader isr =
44 new InputStreamReader(httpConn.getInputStream());
45 BufferedReader in = new BufferedReader(isr);
46 String inputLine;
47 while ((inputLine = in.readLine()) != null)
48 System.out.println(inputLine);
49 in.close();
50 }
51 // copy method from From E.R. Harold's book "Java I/O"
52 public static void copy(InputStream in, OutputStream out)
53 throws IOException {
54 // do not allow other threads to read from the
55 // input or write to the output while copying is
56 // taking place
57 synchronized (in) {
58 synchronized (out) {
59 byte[] buffer = new byte[256];
60 while (true) {
61 int bytesRead = in.read(buffer);
62 if (bytesRead == -1) break;
63 out.write(buffer, 0, bytesRead);
64 }
65 }
66 }
67 }
68}
试了一下,成功。只用两个标准的包就完成了任务,真够简单了。只需把这个程序改成一个过程,就可以在自己的应用中使用了。
该程序在使用的时候要有几个参数:WebService的URL,请求的Soap头部(存在xml文件中),SoapAction(指定要调用的方法)。假设我用.Net开发的WebServiceURL是http://localhost/ws1/service.asmx ,要调用的方法名是GetWeekDay,Soap请求放在getweekday.xml文件中,具体如下:
<?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>
<GetWeekDay xmlns="http://tempuri.org/">
<i>2</i>
</GetWeekDay>
</soap:Body>
</soap:Envelope>
<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>
<GetWeekDay xmlns="http://tempuri.org/">
<i>2</i>
</GetWeekDay>
</soap:Body>
</soap:Envelope>
注意:原文是调用用Java写的WebService,文件中没有第一行。
运行:
返回结果中文会有点乱码,需要处理一下。