SOAP 1.1 を使用しており、Java で SOAP 経由で応答を取得しようとしたときに SOAP UI で正常に動作する WCF Web サービスも作成しました。接続は確立されましたが、データは受信されませんでした。
次の応答がありました。
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);