HTTP send and receive XML requests under Spring MVC

Source: Internet
Author: User
Tags response code stringbuffer
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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.