以下是我写的代码:public class testmy1 { private static String getSoapRequest(String deptid,String password,String xmlstr) {
StringBuilder sb = new StringBuilder();
sb.append("<?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> <SET_XMJB xmlns=\"http://www.scsc.cn/\">"
+ "<DeptID>" + deptid
+ "</DeptID> "
+ "<PassWord>" + password
+ "</PassWord> "
+ "<Query>" + xmlstr
+ "</Query> </SET_XMJB>"
+ "</soap:Body></soap:Envelope>");
return sb.toString();
} /**
* 用户把SOAP请求发送给服务器端,并返回服务器点返回的输入流
*
*/
private static InputStream getSoapInputStream(String deptid,String password,String xmlstr) throws Exception {
try {
String soap = getSoapRequest(deptid,password,xmlstr);
//System.out.print(soap);
if (soap == null) {
return null;
}
URL url = new URL(
"http://124.128.55.20:82/FOR_INTERFACE_SERVICE_SET.asmx");
URLConnection conn = url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true); conn.setRequestProperty("Content-Length", Integer.toString(soap
.length()));
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setRequestProperty("SOAPAction",
"http://www.scsc.cn/SET_XMJB");
OutputStream os = conn.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
osw.write(soap);
osw.flush();
osw.close();
InputStream is = conn.getInputStream();
return is;
} catch (Exception e) {
e.printStackTrace();
return null;
}
} /**
* 对服务器端返回的XML进行解析
*/
public static String getWeather(String deptid,String password,String xmlstr) {
try {
Document doc;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is = getSoapInputStream(deptid,password,xmlstr);
doc = db.parse(is);
NodeList nl = doc.getElementsByTagName("SET_XMJBResult");
String result = nl.item(0).getFirstChild().getNodeValue();
StringBuffer sb = new StringBuffer();
/*for (int count = 0; count < nl.getLength(); count++) {
Node n = nl.item(count);
if (n.getFirstChild().getNodeValue().equals("查询结果为空!")) {
sb = new StringBuffer("#");
break;
}
sb.append(n.getFirstChild().getNodeValue() + "#/n");
}*/
is.close();
//return sb.toString();
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
}
} public static void main(String[] args) throws Exception {
String xmlstr="<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>"
+ "<SET_XMJB xmlns=\"http://cn.scsc.sdgcjs/xsd\">"
+ "<INPUT count=\"1\">"
+ "<ROW>"
+ "<GUID>370600004260344200912N010124</GUID>"
+ "<XMBH>370600004260344200912N010124</XMBH>"
+ "<XMMC>test</XMMC>"
+ "<SPDW>test</SPDW>"
+ "<XMGK>test</XMGK>"
+ "<JSSJ>2011-8-11 12:05:00</JSSJ>"
+ "<JSDW>test</JSDW>"
+ "<JSDWDM>04260344</JSDWDM>"
+ "<XMFR>test</XMFR>"
+ "<CJDW>test</CJDW>"
+ "<FBDW>test</FBDW>"
+ "<FBSJ>2011-8-11 12:05:00</FBSJ>"
+ "<FLAG>0</FLAG>"
+ "</ROW>"
+ "</INPUT>"
+ "</SET_XMJB>";
String deptid="1111111111";
String password="2222222222";
System.out.println(getWeather(deptid,password,xmlstr));
}
}
运行后报错java.io.IOException: Server returned HTTP response code: 400 for URL: http://124.128.55.20:82/FOR_INTERFACE_SERVICE_SET.asmx
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at com.test731.getSoapInputStream(test731.java:91)
at com.test731.getResult(test731.java:107)
at com.test731.main(test731.java:149)
java.lang.IllegalArgumentException: InputStream cannot be null
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at com.test731.getResult(test731.java:111)
at com.test731.main(test731.java:149)
null我发现问题好像是出现在 InputStream is = conn.getInputStream(); 这句上谁能帮我解决下这个问题下,谢谢了
StringBuilder sb = new StringBuilder();
sb.append("<?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> <SET_XMJB xmlns=\"http://www.scsc.cn/\">"
+ "<DeptID>" + deptid
+ "</DeptID> "
+ "<PassWord>" + password
+ "</PassWord> "
+ "<Query>" + xmlstr
+ "</Query> </SET_XMJB>"
+ "</soap:Body></soap:Envelope>");
return sb.toString();
} /**
* 用户把SOAP请求发送给服务器端,并返回服务器点返回的输入流
*
*/
private static InputStream getSoapInputStream(String deptid,String password,String xmlstr) throws Exception {
try {
String soap = getSoapRequest(deptid,password,xmlstr);
//System.out.print(soap);
if (soap == null) {
return null;
}
URL url = new URL(
"http://124.128.55.20:82/FOR_INTERFACE_SERVICE_SET.asmx");
URLConnection conn = url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true); conn.setRequestProperty("Content-Length", Integer.toString(soap
.length()));
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setRequestProperty("SOAPAction",
"http://www.scsc.cn/SET_XMJB");
OutputStream os = conn.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
osw.write(soap);
osw.flush();
osw.close();
InputStream is = conn.getInputStream();
return is;
} catch (Exception e) {
e.printStackTrace();
return null;
}
} /**
* 对服务器端返回的XML进行解析
*/
public static String getWeather(String deptid,String password,String xmlstr) {
try {
Document doc;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is = getSoapInputStream(deptid,password,xmlstr);
doc = db.parse(is);
NodeList nl = doc.getElementsByTagName("SET_XMJBResult");
String result = nl.item(0).getFirstChild().getNodeValue();
StringBuffer sb = new StringBuffer();
/*for (int count = 0; count < nl.getLength(); count++) {
Node n = nl.item(count);
if (n.getFirstChild().getNodeValue().equals("查询结果为空!")) {
sb = new StringBuffer("#");
break;
}
sb.append(n.getFirstChild().getNodeValue() + "#/n");
}*/
is.close();
//return sb.toString();
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
}
} public static void main(String[] args) throws Exception {
String xmlstr="<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>"
+ "<SET_XMJB xmlns=\"http://cn.scsc.sdgcjs/xsd\">"
+ "<INPUT count=\"1\">"
+ "<ROW>"
+ "<GUID>370600004260344200912N010124</GUID>"
+ "<XMBH>370600004260344200912N010124</XMBH>"
+ "<XMMC>test</XMMC>"
+ "<SPDW>test</SPDW>"
+ "<XMGK>test</XMGK>"
+ "<JSSJ>2011-8-11 12:05:00</JSSJ>"
+ "<JSDW>test</JSDW>"
+ "<JSDWDM>04260344</JSDWDM>"
+ "<XMFR>test</XMFR>"
+ "<CJDW>test</CJDW>"
+ "<FBDW>test</FBDW>"
+ "<FBSJ>2011-8-11 12:05:00</FBSJ>"
+ "<FLAG>0</FLAG>"
+ "</ROW>"
+ "</INPUT>"
+ "</SET_XMJB>";
String deptid="1111111111";
String password="2222222222";
System.out.println(getWeather(deptid,password,xmlstr));
}
}
运行后报错java.io.IOException: Server returned HTTP response code: 400 for URL: http://124.128.55.20:82/FOR_INTERFACE_SERVICE_SET.asmx
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at com.test731.getSoapInputStream(test731.java:91)
at com.test731.getResult(test731.java:107)
at com.test731.main(test731.java:149)
java.lang.IllegalArgumentException: InputStream cannot be null
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at com.test731.getResult(test731.java:111)
at com.test731.main(test731.java:149)
null我发现问题好像是出现在 InputStream is = conn.getInputStream(); 这句上谁能帮我解决下这个问题下,谢谢了
你这个调用WS的方式,是自己设计的,还是从别的地方学习的?建议用专门的WSClient来做会安全些。
哪怕是用Java自带的支持都很好,参见:
http://yang-min.iteye.com/blog/600172
Host: 124.128.55.20
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.scsc.cn/SET_XMJB"<?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>
<SET_XMJB xmlns="http://www.scsc.cn/">
<DeptID>string</DeptID>
<PassWord>string</PassWord>
<Query>xml</Query>
</SET_XMJB>
</soap:Body>
</soap:Envelope>是不是这两个要求一致,我就是把相应的 <DeptID>string</DeptID>
<PassWord>string</PassWord>
<Query>xml</Query> 这三个变成相关内容了,谁能帮我看看这有什么问题呢,谢谢了