HTTP send and receive XML requests under Spring MVCThis article describes how to send and receive XML requests through HTTP, in conjunction with the Swagger plug-in, under the Spring MVC Framework. 1, through the control to write, receive the interface of the XML request,
Where @requestbody tags, mainly in the content of XML body to see the direct transfer of XML content
Where @requestheader store the head content in XML, mainly used for interface security verification
The first half verifies the interface, the latter part parses the XML data, and the XML can be parsed with dom4j.
The code is as follows: 2, Swagger Interface interface, user-friendly, the code is as follows: 3, through HTTP, send an XML request.
public string dopostbycontent (string url, string param,map<string,object> headermap) throws Exception {
/* Translate parameter map to parameter date String */
StringBuffer parameterbuffer = new StringBuffer ();
Parameterbuffer.append (param);
URL localurl = new URL (URL);
URLConnection connection = OpenConnection (Localurl);
HttpURLConnection httpurlconnection = (httpurlconnection) connection;
Httpurlconnection.setdooutput (TRUE);
Httpurlconnection.setrequestmethod ("POST");
Httpurlconnection.setrequestproperty ("Content-type", "application/json;charset=utf-8");
Httpurlconnection.setrequestproperty ("Accept-charset", Charset);
Httpurlconnection.setrequestproperty ("Content-length", String.valueof (Parameterbuffer.length ()));
if (Headermap!=null) {
Iterator<string> iterator = Headermap.keyset (). iterator ();
String key = null;
String value = null;
while (Iterator.hasnext ()) {
Key = (String) iterator.next ();
if (Headermap.get (key)!=null) {
Value = (String) headermap.get (key);
} else {
Value = "";
}
Httpurlconnection.setrequestproperty (Key,value);
}
}
OutputStream outputstream = null;
InputStream inputstream = null;
InputStreamReader inputstreamreader = null;
BufferedReader reader = null;
StringBuffer resultbuffer = new StringBuffer ();
String templine = null;
try {
String s = parameterbuffer.tostring ();
OutputStream = Httpurlconnection.getoutputstream ();
Byte[]data =parameterbuffer.tostring (). GetBytes ();
Outputstream.write (data);
if (Httpurlconnection.getresponsecode () >= 300) {
throw new Exception ("HTTP Request is isn't success, Response code is" +httpurlconnection.getresponsecode ());
}
InputStream = Httpurlconnection.getinputstream ();
InputStreamReader = new InputStreamReader (InputStream, "utf-8");
reader = new BufferedReader (InputStreamReader);
while ((Templine =reader.readline ())!=null) {
Resultbuffer.append (Templine);
}
finally {
if (OutputStream!=null) {
Outputstream.close ();
}
if (reader!=null) {
Reader.close ();
}
if (InputStreamReader!=null) {
Inputstreamreader.close ();
}
if (InputStream!=null) {
Inputstream.close ();
}
}
return resultbuffer.tostring ();
} 4 Head storage: