java sends soap request and parses soap response
1. soap request message
<s:Envelope
xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<GetPoints xmlns="http://tempuri.org/">
<kunkun xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<d4p1:string>chang</d4p1:string>
<d4p1:string>tiao</d4p1:string>
<d4p1:string>rap</d4p1:string>
<d4p1:string>basketball</d4p1:string>
</kunkun>
</GetPoints>
</s:Body>
</s:Envelope>
2. soap response message
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<GetPointsResponse xmlns="http://tempuri.org/">
<GetPointsResult xmlns:a="http://schemas.datacontract.org/2004/07/XDB.DataServiceInterface" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:Data>
<a:Name>张三</a:Name>
<a:LikeYear>2.5年</a:LikeYear>
</a:Data>
<a:Data>
<a:Name>李四</a:Name>
<a:LikeYear>2.5年</a:LikeYear>
</a:Data>
<a:Data>
<a:Name>王二</a:Name>
<a:LikeYear>2.5年</a:LikeYear>
</a:Data>
<a:Data>
<a:Name>麻子</a:Name>
<a:LikeYear>2.5年</a:LikeYear>
</a:Data>
</GetPointsResult>
</GetPointsResponse>
</s:Body>
</s:Envelope>
3. Code implementation
1. Introduce the corresponding jar
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
</dependency>
2. Code implementation
public boolean testSoap() {
StringBuilder result = new StringBuilder();
OutputStream out = null;
BufferedReader in = null;
String[] pointNames = {
"chang","tiao","rap","basketball"};
String soap = buildXML(pointNames);
try {
URL url = new URL(你需要请求的url地址);
URLConnection connection = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) connection;
byte[] b = soap.getBytes("ISO-8859-1");
httpConn.setRequestProperty( "Content-Length",String.valueOf( b.length ) );
httpConn.setRequestProperty("Content-Type","text/xml; charset=utf-8");
httpConn.setRequestProperty("soapaction","http://tempuri.org/WcfDataProxy/GetPoints");
httpConn.setRequestMethod( "POST" );
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
out = httpConn.getOutputStream();
out.write( b );
in = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
result.append(inputLine);
out.close();
in.close();
Document doc = DocumentHelper.parseText(result.toString());
Element root = doc.getRootElement();
Map<String, String> map = new HashMap<String, String>();
List<Map<String, String>> lists = new ArrayList<Map<String, String>>();
getCode(root, map,lists);
for (Map<String, String> item : lists) {
String key = item.get("Name");
String value = item.get("LikeYear");
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (org.dom4j.DocumentException e) {
e.printStackTrace();
} finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return true;
}
private static String buildXML(String[] pointNames){
String str = "<s:Envelope\n" +
" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
" <s:Body>\n" +
" <GetPoints\n" +
" xmlns=\"http://tempuri.org/\">\n" +
" <kunkun\n" +
" xmlns:d4p1=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\"\n" +
" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">\n";
for (String s : pointNames) {
str += "<d4p1:string>" + s + "</d4p1:string>"+"\n";
}
str += "</kunkun>\n" +
" </GetPoints>\n" +
" </s:Body>\n" +
"</s:Envelope>";
return str;
}
public static void getCode(Element root, Map<String, String> map,List<Map<String, String>> lists) {
if (root.elements() != null) {
List<Element> list = root.elements();
for (Element e : list) {
if (e.elements().size() > 0) {
getCode(e, map ,lists);
}
if (e.elements().size() == 0) {
map.put(e.getName(), e.getTextTrim());
if (map.size() == 2){
Map<String, String> mapTo = new HashMap<String, String>();
mapTo.putAll(map);
lists.add(mapTo);
map.clear();
}
}
}
}
}