Java 访问 .net WebService 接口示例

本文介绍两种使用Java访问.NET WebService接口的方法:一种是通过普通的HTTP POST请求方式发送SOAP消息;另一种则是利用Java内置的SOAP API进行交互。文中提供了完整的代码示例,包括如何构造SOAP请求、设置SOAPAction头以及解析返回结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
		}
	}
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值