连接到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();
}
------解决思路----------------------
试试用HttpClient