Java 访问 .net WebService 接口示例
A.XML Code:
<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Header> <KytSoapHeader xmlns="http://www.kyt56.cn/"> <UserName>admin</UserName> <PassWord>admin</PassWord> </KytSoapHeader> </soap:Header> <soap:Body> <GetDeliveryListByStatus xmlns="http://www.kyt56.cn/" /> </soap:Body> </soap:Envelope>
第一种方式,普通访问网页的方式
JavaCode:
public static void main(String[] args) {
try {
String urlString = "http://localhost:8090/ws/DeliveryList.asmx";
String xmlFile = "d:\\A.XML";
String soapActionString = "http://www.kyt56.cn/GetDeliveryListByStatus";
File fileToSend = new File(xmlFile);
byte[] buf = new byte[(int) fileToSend.length()];
new FileInputStream(xmlFile).read(buf);
URL url = new URL(urlString);
HttpURLConnection httpConn = (HttpURLConnection) url
.openConnection();
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("Content-Length", String
.valueOf(buf.length));
httpConn.setRequestProperty("Content-Type",
"text/xml; charset=utf-8");
httpConn.setRequestProperty("SOAPAction", soapActionString);
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
OutputStream out = httpConn.getOutputStream();
out.write(buf);
out.close();
InputStreamReader isr = new InputStreamReader(httpConn
.getInputStream(), "utf-8");
BufferedReader in = new BufferedReader(isr);
String inputLine;
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("D:\\result.xml")));
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine.replaceAll("><", ">\n<"));
bw.write(inputLine);
bw.newLine();
}
bw.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
第二种方式,java内置soap访问方式
JavaCode
package soap;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
public class SOAPMain {
public static void main(String[] args) {
String url = "http://localhost:8090/ws/DeliveryList.asmx";
String SOAPAction ="http://www.kyt56.cn/GetDeliveryListByStatus";
String UserName="sadf";
String PassWord="asdf";
try {
// 设置SOAP内容格式
String soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ "<soap:Header /><soap:Body /></soap:Envelope>";
InputStream is = new ByteArrayInputStream(soap.getBytes("UTF-8"));
// 创建message对象
MessageFactory mf =MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
SOAPMessage message = mf.createMessage(null, is);
// 设置SOAPAction
message.getMimeHeaders().addHeader("SOAPAction",SOAPAction);
//设置请求字符格式
message.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "utf-8");
// 获取envelope
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
// 设置 header KytSoapHeader 用户名和密码
SOAPHeader header = envelope.getHeader();
SOAPElement headerElement = header.addChildElement(envelope
.createName("KytSoapHeader","", "http://www.kyt56.cn/"));
headerElement.addChildElement("UserName").addTextNode(UserName);
headerElement.addChildElement("PassWord").addTextNode(PassWord);
//设置Body
SOAPBody body = envelope.getBody();
// 保存Message
message.saveChanges();
//开始访问接口
//创建连接对象
SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
//发送请求
SOAPMessage reply = connection.call(message, url);
//关闭连接对象
connection.close();
//访问结束开始处理结果
//获取节点 KYTResults
org.w3c.dom.Node node = reply.getSOAPBody().getFirstChild().getFirstChild().getFirstChild();
System.out.println("Node Name:"+node.getLocalName()+"\n");
System.out.println("Node Text:"+node.getTextContent());
//System.out.println(((SOAPElement)reply.getSOAPBody().getChildElements().next()).getTextContent());
System.out.println("\nResponse Content:");
//将读取的内容读出来
ByteArrayOutputStream out= new ByteArrayOutputStream();
reply.writeTo(out);
InputStreamReader isr = new InputStreamReader(new ByteArrayInputStream(out.toByteArray()), "utf-8");
BufferedReader br = new BufferedReader(isr);
while (br.ready()) {
//String inputLine;
System.out.println(br.readLine().replaceAll("><", ">\n<"));
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}