以下是我写的代码: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();  这句上谁能帮我解决下这个问题下,谢谢了

解决方案 »

  1.   

    跟InputStream无直接关系。HTTP 400 表示:服务器端无法识别你所发出的请求内容,也就是认为你发送的请求信息是不合规的。
    你这个调用WS的方式,是自己设计的,还是从别的地方学习的?建议用专门的WSClient来做会安全些。
    哪怕是用Java自带的支持都很好,参见:
    http://yang-min.iteye.com/blog/600172
      

  2.   

    是根据一个调用天气预报的例子改的,关键是要和现有的java程序结合,请教一下,这种问题我应该从什么地方查找错误呢
      

  3.   

    你的意思 是 soap 的内容和请求的不一致吗?同样的程序调用天气预报的可以,换成现在请求的就发生错误,问题是发生在报文上吗,程序的写法有没有什么问题呢?
      

  4.   

    使用SoapUI查看下的Webservice请求的封装内容是什么,然后使用HttpConnection再尝试发送请求
      

  5.   

    你先说一下你知道不知道你的那个StringBuilder 拼接出的xml代表什么
      

  6.   

    StringBuilder  拼装成的是soap的文件,这个文件是按照http://124.128.55.20:82/FOR_INTERFACE_SERVICE_SET.asmx地址中 的SET_XMJB方法写的
      

  7.   

    StringBuilder  拼装成的是soap的文件,这个文件是按照http://124.128.55.20:82/FOR_INTERFACE_SERVICE_SET.asmx地址中 的SET_XMJB方法写的
      

  8.   

    以下是生成的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:Body><SET_XMJB xmlns="http://www.scsc.cn/"><DeptID>444444</DeptID><PassWord>4444444</PassWord><Query><?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></Query></SET_XMJB></soap:Body></soap:Envelope>下面是http://124.128.55.20:82/FOR_INTERFACE_SERVICE_SET.asmx地址中 的SET_XMJB方法的soapPOST /FOR_INTERFACE_SERVICE_SET.asmx HTTP/1.1
    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> 这三个变成相关内容了,谁能帮我看看这有什么问题呢,谢谢了
      

  9.   

    你先把SET_XMJB的WSDL发一下吧。