12,166
社区成员




URL url = new URL("http://www.webxml.com.cn/WebServices/WeatherWS.asmx" );
String soap_xml = soap_xml(city,"");
System.out.println(soap_xml);
HttpURLConnection httpURLConnection=(java.net.HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
//使用http进行输出
httpURLConnection.setDoOutput(true);
//使用http进行输入
httpURLConnection.setDoInput(true);
//通过输出流发送数据
OutputStream outputStream = httpURLConnection.getOutputStream();
outputStream.write(soap_xml.getBytes());
outputStream.close();
//接收服务端响应数据
InputStream inputStream = httpURLConnection.getInputStream();
//使用buffer读取存在的数据
byte[] buffer = new byte[1024];
//使用字节输出流存储读取的数据
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
while(true){
int len = inputStream.read(buffer);
//如果流水读取完则退出循环
if(len == -1){
break;
}
byteArrayOutputStream.write(buffer,0,len);
}
//得到响应数据
String response_string = byteArrayOutputStream.toString();
<?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>
<getWeather xmlns="http://WebXml.com.cn/">
<theCityCode>北京</theCityCode>
<theUserID></theUserID>
</getWeather>
</soap:Body>
</soap:Envelope>
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<getWeatherResponse xmlns="http://WebXml.com.cn/">
<getWeatherResult>
<string>查询结果为空。http://www.webxml.com.cn/</string>
</getWeatherResult>
</getWeatherResponse>
</soap:Body>
</soap:Envelope>