简单 SOAP 客户机:通用 Java SOAP 客户机
2010-03-23 00:00:00 来源:WEB开发网因为该 SOAP 客户机使用 HTTP 协议发送 XML SOAP 请求,所以大量必需做的工作就是 HTTP 设置。Java 提供了一个 HttpURLConnection 类, 它有许多“设置”方法来正确设置每个 HTTP 参数,并且可以用简单的字符串来设置大多数参数。 需要一点额外代码的一个 HTTP 参数是 Content-Length ,所以 SoapClient4XG 通过在读取 XML 请求之后将它放到一个字节数组中,然后检查字节数组的 长度 特性来计算 XML 请求的长度。
可使用其它会代您设置这些 HTTP 参数的 HTTP 实现。Sun 开放源码的 Brazil Web 应用程序框架会自动处理 HTTP 问题, 并使处理适当 SOAP 错误更为容易,因为(不象早期的 HttpURLConnection 类)它是一个没有经过特定编写以用 Java 小应用程序减轻装入图像和其它 Web 资源工作的通用 HTTP 类。
请参考“清单 1”,获取 完整的 SOAP 客户机。
Listing 1. The complete SOAP client
/**
* 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);
}
}
}
}
}
更多精彩
赞助商链接