java socket测试webservice是不是可连接
java socket测试webservice是否可连接
如果要事先测试webservice服务是否可用,可以用java socket连接wsdl地址,相当于模拟浏览器输入wsdl地址。
我在本机上发布了一个webservice,用于下载文件,wsdl是:
http://127.0.0.1:8080/FileWS/services/downloadFile?wsdl
Socket socket = new Socket("127.0.0.1",8080);
OutputStream os = socket.getOutputStream();
InputStream is = socket.getInputStream();
String httpSend = "POST /FileWS/services/downloadFile?wsdl HTTP/1.1\r\n"
+ "Content-Type:text/xml\r\n"
+ "Host:127.0.0.1:8080\r\n"
+ "Content-Length:454\r\n"
+ "\r\n"
+ "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"
+ "<SOAP-ENV:Body>"
+ " </SOAP-ENV:Body>"
+ "</SOAP-ENV:Envelope>";
os.write(httpSend.getBytes());
os.flush();
InputStreamReader ireader = new InputStreamReader(is);
java.io.BufferedReader breader = new java.io.BufferedReader(ireader);
String responseLine = "";
while(!(responseLine = breader.readLine()).equals(""))
{
System.out.println(responseLine);
}
System.out.println("");
while(!(responseLine = breader.readLine()).equals(""))
{
System.out.println(responseLine);
}
启动webservice,运行该代码,就会打印。如果看到http/1.1 200 ok,就连接成功了。控制台输出的内容和在浏览器输入wsdl地址得到的内容一致。如果有error,注意检查自己的wsdl是否可以在浏览器正确输出,监测发送的字符串,注意转义字符等等。
如果要事先测试webservice服务是否可用,可以用java socket连接wsdl地址,相当于模拟浏览器输入wsdl地址。
我在本机上发布了一个webservice,用于下载文件,wsdl是:
http://127.0.0.1:8080/FileWS/services/downloadFile?wsdl
Socket socket = new Socket("127.0.0.1",8080);
OutputStream os = socket.getOutputStream();
InputStream is = socket.getInputStream();
String httpSend = "POST /FileWS/services/downloadFile?wsdl HTTP/1.1\r\n"
+ "Content-Type:text/xml\r\n"
+ "Host:127.0.0.1:8080\r\n"
+ "Content-Length:454\r\n"
+ "\r\n"
+ "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"
+ "<SOAP-ENV:Body>"
+ " </SOAP-ENV:Body>"
+ "</SOAP-ENV:Envelope>";
os.write(httpSend.getBytes());
os.flush();
InputStreamReader ireader = new InputStreamReader(is);
java.io.BufferedReader breader = new java.io.BufferedReader(ireader);
String responseLine = "";
while(!(responseLine = breader.readLine()).equals(""))
{
System.out.println(responseLine);
}
System.out.println("");
while(!(responseLine = breader.readLine()).equals(""))
{
System.out.println(responseLine);
}
启动webservice,运行该代码,就会打印。如果看到http/1.1 200 ok,就连接成功了。控制台输出的内容和在浏览器输入wsdl地址得到的内容一致。如果有error,注意检查自己的wsdl是否可以在浏览器正确输出,监测发送的字符串,注意转义字符等等。