我做的是用SOAP传输XML信息到服务器。我觉得代码没有问题,但总是报以下错误。
我用的是JDK1.6 Tomcat 5.0 Eclipse 3.1.2 操作系统是Windows Server 2003
程序清单和运行结果如下所示。
程序如下:
package cn;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class SOAPClient4XG {
public static void main(String[] args) throws Exception {
String SOAPUrl = "http://localhost:8080/soap/servlet/rpcrouter ";
String xmlFile2Send = "src/cn/weattherreq.xml ";
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);
System.out.println(fin);
System.out.println( "............................................... ");
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);
System.out.println( "111111111111111111111111111 ");
// Everything 's set up; send the XML that was read in to b.
OutputStream out = httpConn.getOutputStream();
out.write(b);
out.close();
System.out.println( "2222222222222222222222222 ");
// 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);
}
}
}
}
}
运行后的结果为:
java.io.FileInputStream@530daa
...............................................
111111111111111111111111111
2222222222222222222222222
Exception in thread "main " java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:8080/soap/servlet/rpcrouter
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at cn.SOAPClient4XG.main(SOAPClient4XG.java:59)
500 是服务器错误,现用浏览器访问看看是否能得到数据
tomcat的默认端口改掉了~~~