我们使用的是 SOAP 1.1,并且我们还创建了一个 WCF Web 服务,当我们尝试通过 Java 中的 SOAP 获取响应时,它在 SOAP UI 中运行良好,连接已建立,但未收到任何数据。
我们得到了这样的回应:
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Encoding: gzip
Vary: Accept-Encoding
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Tue, 16 Oct 2012 12:05:28 GMT
Content-Length: 148
在此之后没有收到任何 SOAP 响应并引发此错误:
ERROR : java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:189)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:154)
at java.io.BufferedReader.readLine(BufferedReader.java:317)
at java.io.BufferedReader.readLine(BufferedReader.java:382)
at ICNumber.main(ICNumber.java:113)
这是代码片段:
String SOAPRequest ="<soapenv:Envelope"+
"xmlns:soapenv"+
"=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">"+
"<soapenv:Header>"+
"</soapenv:Header>"+
"<soapenv:Body>"+
"<tem:ICNumberValidation>"+
"<tem:ICNumber>"+
strIC+
"</tem:ICNumber>"+
"</tem:ICNumberValidation>"+
"</soapenv:Body>"+
"</soapenv:Envelope>";
try {
//Create socket
String hostname = "192.168.1.xx";
int port = 90;
InetAddress addr = InetAddress.getByName(hostname);
SocketAddress sockaddr = null;
try
{
sockaddr = new InetSocketAddress(addr, (port));
}
catch(IllegalArgumentException e)
{
System.out.println("ErrorCode-6001");
}
Socket sock = new Socket();
try
{
sock.connect(sockaddr, 10000);
}
catch(Exception e)
{
System.out.println( "ErrorCode-6001");
}
//Send header
String path = "/Service1.svc";
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream(),"UTF-8"));
// You can use "UTF8" for compatibility with the Microsoft virtual machine.
wr.write("POST " + path + " HTTP/1.1\r\n");
wr.write("Accept-Encoding: gzip,deflate\r\n");
wr.write("Host: " + hostname + "\r\n");
wr.write("Content-Length: " + SOAPRequest.length() + "\r\n");
wr.write("Content-Type: text/xml; charset=\"utf-8\"\r\n");
wr.write("SOAPAction: \"http://tempuri.org/IService1/ICNumberValidation\"\r\n");
wr.write("Connection: Keep-Alive\r\n");
wr.write("User-Agent: Apache-HttpClient/4.1.1 (java 1.5)\r\n");
wr.write("\r\n");
//Send data
wr.write(SOAPRequest);
wr.flush();
//response read
BufferedReader reader = new BufferedReader( new InputStreamReader( sock.getInputStream() ) );
String line = "";
String line2 = "";
while((line = reader.readLine()) != null)
{
line2 = line2 + line+"\n";
System.out.println(line2);
}
System.out.println(line2);