java中不用框架实现连接webservice的问题

J2EE 码拜 9年前 (2015-04-03) 1002次浏览 0个评论
 

连接到webxml.com中的列车时刻表服务。

我自己写的代码如下,运行结果为:
200
3067
null
sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@750159
[#document: null]
#document
null

请帮忙看看问题出在什么地方??
我自己感觉请求已经发出,而且已经有了返回响应,不晓得为什么读不出来?

String s="<?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:Body>"
              +"<getDetailInfoByTrainCode xmlns="http://WebXml.com.cn/">"
              +"<TrainCode>T5686</TrainCode>"
              +"<UserID></UserID>"
              +"</getDetailInfoByTrainCode>"
              +"</soap:Body>"
              +"</soap:Envelope>";
	HttpURLConnection urlConnection=null;
	try {
		URL url=new URL("http://webservice.webxml.com.cn/WebServices/TrainTimeWebService.asmx");
		urlConnection=(HttpURLConnection)url.openConnection();
		urlConnection.setDoInput(true);
		urlConnection.setDoOutput(true);
		urlConnection.setRequestMethod("POST");
		urlConnection.setRequestProperty("Host", "webservice.webxml.com.cn");
		urlConnection.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
		urlConnection.setRequestProperty("Content-Length", s.length()+"");
		urlConnection.setRequestProperty("SOAPAction", "http://WebXml.com.cn/getDetailInfoByTrainCode");

		OutputStream os=urlConnection.getOutputStream();
		OutputStreamWriter osw=new OutputStreamWriter(os, "utf-8");
		osw.write(s);
		osw.flush();
		osw.close();
		System.out.println(urlConnection.getResponseCode());
		System.out.println(urlConnection.getContentLength());
		System.out.println(urlConnection.getContentEncoding());

		InputStream is=urlConnection.getInputStream();
		System.out.println(is.toString());
		org.w3c.dom.Document doc;
		DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
		dbf.setNamespaceAware(true);
		DocumentBuilder db=dbf.newDocumentBuilder();
		doc=db.parse(is);
		System.out.println(doc.toString());
		System.out.println(doc.getNodeName());
		NodeList n1=doc.getElementsByTagName("getDetailInfoByTrainCodeResult");
		Node n=n1.item(0);
		System.out.println(n.getFirstChild().getNodeValue());
	} catch (MalformedURLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (ParserConfigurationException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (SAXException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
java中不用框架实现连接webservice的问题
40分
试试用HttpClient

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明java中不用框架实现连接webservice的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!