最近在写大华webservice对接,因为无法实测,所以写了个调用天气的webservice进行测试
public JSONObject sendReq(String... strings) {
byte[] content;
HttpClient httpClient = new HttpClient();
String deivceListStr = "";
JSONObject jsonObject = new JSONObject();
JSONObject deviceTreeJson = new JSONObject();
JSONArray jsonArray = new JSONArray();
JSONArray newJsonArray = new JSONArray();
Document doc = null;
try {
String str = XmlUtils.getXML(strings);
content = str.getBytes("UTF-8");
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
url="http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl";
PostMethod method = new PostMethod(url);
httpClient.getParams().setContentCharset("UTF-8");
method.setRequestHeader("Connection", "close");
RequestEntity requestEntity = new ByteArrayRequestEntity(content,
"text/xml;charset=UTF-8");
method.setRequestEntity(requestEntity);
int statusCode = httpClient.executeMethod(method);
if (statusCode == 200) {
//这是调用成功的返回结果,即下方的xmlstr
//<?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><getSupportCityResponse xmlns="http://WebXml.com.cn/">
// <getSupportCityResult><string>北京 (54511)</string><string>上海 (58367)</string><string>天津 (54517)</string><string>重庆 (57516)</string></getSupportCityResult></getSupportCityResponse></soap:Body></soap:Envelope>
String xmlStr = method.getResponseBodyAsString();
logger.error("xmlStr打印为" + xmlStr);
doc = DocumentHelper.parseText(xmlStr);
deviceTreeJson.put("channels", newJsonArray);
}
} catch (Exception e) {
e.printStackTrace();
}
return deviceTreeJson;
}
public static String getXML(String... strings){
//因为测试,这个方法直接写死了
xml="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://WebXml.com.cn/\">" +
" <soapenv:Header/>" +
" <soapenv:Body>" +
" <web:getSupportCityDataset>" +
" <!--Optional:-->" +
" <web:theRegionCode>江苏</web:theRegionCode>" +
" </web:getSupportCityDataset>" +
" </soapenv:Body>" +
"</soapenv:Envelope>";
return xml;
}