public static void Main (string[] args) {
String xml = "<?xml version="
+ "\" 1.0\ ""
+ "encoding="
+ "\" Utf-8\ ""
+ "? ><sdrequest><transactionname>createdatafilecomplete</transactionname><identityinfo ><Code> "
+ 1 + "</Code><Description></Description><Timestamp>"
+ "20100315140542" + "</Timestamp></IdentityInfo></SDRequest>";//New Project interface, not to use XML request, no other post method found And, finally, this is the way to spell the parameters into an XML string
File input = new file ("Test.xml");//If it is an XML file, you can write this
Postmethod post = new Postmethod ("http://localhost/site/forXls.do");//Request Address
Sets the content of the request to be read directly from the file
Post.setrequestbody (NewFileInputStream (input));
//If(Input.length () < Integer.max_value)
Post.setrequestcontentlength (Input.length ());
//Else
Post.setrequestcontentlength (entityenclosingmethod.content_length_chunked);
Post.setrequestbody (XML);//Add an XML string here
Specify the type of the requested content
Post.setrequestheader ("Content-type", "text/xml; CHARSET=GBK ");
HttpClient HttpClient = new HttpClient ();//Create an instance of HttpClient
int result;
try {
result = Httpclient.executemethod (POST);
System.out.println ("Response Status code:" + result);//returns 200 for success
SYSTEM.OUT.PRINTLN ("Response body:");
System.out.println (Post.getresponsebodyasstring ());//content returned
Post.releaseconnection ();//Release connection
} catch (HttpException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
The above is the code to send the request, the other side received the data can be directly parsed into XML
Document airdocument = getclientrequestmessage (request);
System.out.println (Airdocument.getrootelement (). GetName ());
if ((xmlinterfaceparam.sd_request). Equals (Airdocument.getrootelement ()
. GetName ())) {
Element tn = Airdocument.getrootelement (). Getchild (
Xmlinterfaceparam.transaction_name);
if ("Createdatafilecomplete". Equals (Tn.gettext ())) {
Element II = airdocument.getrootelement (). Getchild (
Xmlinterfaceparam.identity_info);
String code = ii.getchildtext (Xmlinterfaceparam.code);
String Description = II
. Getchildtext (Xmlinterfaceparam.description);
String timestamp = Ii.getchildtext (Xmlinterfaceparam.timestamp);
System.out.println ("code:" + code);
System.out.println ("description:" + description);
System.out.println ("timestamp:" + timestamp);
}
}
Read XML
Private Document Getclientrequestmessage (HttpServletRequest _request)
Throws Unexpectedexception {
try {
Saxbuilder builder = new Saxbuilder ();
InputSource is = new InputSource (); Create an input
Source
Is.setbytestream (_request.getinputstream ()); Set the input
Stream mandated
To UTF-8
Is.setencoding ("UTF-8"); Set the mandate
Encoding to the input
Source
Document document = Builder.build (IS);
return document;
} catch (IOException e) {
E.printstacktrace ();
throw New Unexpectedexception (
"IOException exception when getinputstream from HTTP request",
e);
} catch (Jdomexception e) {
E.printstacktrace ();
throw New Unexpectedexception (
"Jdomexception when build document form InputStream", e);
} catch (NullPointerException e) {
E.printstacktrace ();
throw New Unexpectedexception (
"NullPointerException when build document form InputStream",
e);
} catch (ClassCastException e) {
E.printstacktrace ();
throw New Unexpectedexception (
"ClassCastException when build document form InputStream",
e);
}
}
HttpClient sending an XML string (push)