直接上代码
String address =“url”;
URL u = new URL(address);
HttpURLConnection uc = (HttpURLConnection) u.openConnection();
uc.setRequestProperty(“Content-Type”, “text/xml; charset=utf-8”);
uc.setRequestMethod(“POST”);
uc.setUseCaches(false);
uc.setDoOutput(true);
uc.setDoInput(true);
StringBuffer sb = new StringBuffer();
//报文头
sb.append("<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/\">\n" +
" <soap:Body>\n" +
" <getRecipeRegex xmlns=\"http://tempuri.org/\">\n" +
" <recipeNmae>" + recipeName+ "</recipeNmae>\n"+
" <deviceCode>" + deviceCode+ "</deviceCode>\n"+
" </getRecipeRegex>\n" +
" </soap:Body>\n" +
"</soap:Envelope>");
OutputStreamWriter osw = new OutputStreamWriter(uc.getOutputStream(), "UTF-8");
osw.write(sb.toString());
osw.flush();
osw.close();
BufferedReader rd = new BufferedReader(new InputStreamReader(uc.getInputStream(), "UTF-8"));
StringBuffer sbuffer = new StringBuffer();
int ch;
while ((ch = rd.read()) > -1) {
sbuffer.append((char) ch);
}
//返回值
String rec_string = "";
rec_string = sbuffer.toString().trim();
JSONObject result = JSONObject.fromObject(rec_string.substring(rec_string.indexOf("{", 0), rec_string.lastIndexOf("}", rec_string.length()) + 1));
String resultValue = result.getString("resultValue").trim();
String message = result.getString("message").trim();
Map map = new HashMap();
map.put("resultValue", resultValue);
map.put("message", message);
return map;