You can load or read an XML file to obtain its data, and then treat the obtained data as an entity, and send it to the server using the output stream through the HTTP protocol, on the server side, you can obtain the relevant data by obtaining the input stream. In this way, you can send XML data to the server. As follows:
Client:
Public void sendxmltest () throws exception {
// Load XML resources through the Class Loader
Inputstream = This. getclass (). getclassloader (). getresourceasstream ("test. xml ");
Byte [] xml = streamtool. Read (inputstream );
String Path = "http: // 172.22.35.112: 8080/videonews/getxmlinfo ";
URL url = new URL (PATH );
Httpurlconnection conn = (httpurlconnection) URL. openconnection ();
Conn. setrequestmethod ("Post ");
Conn. setconnecttimeout (5000 );
Conn. setdooutput (true );
// Set the HTTP request header field
Conn. setrequestproperty ("Content-Type", "text/XML; charset = UTF-8"); // content type
Conn. setrequestproperty ("Content-Length", String. valueof (XML. Length); // The length of the object content
Conn. getoutputstream (). Write (XML); // write data to the server through the output stream
If (conn. getresponsecode () = 200 ){
System. Out. println ("sent successfully! ");
} Else {
System. Out. println ("failed to send! ");
}
}
Server:
- Byte [] xml = streamtool. Read (request. getinputstream (); // get the output stream
- System. Out. println (new string (XML, "UTF-8 "));