For Java with a send HTTP request, the request content is in XML format

Source: Internet
Author: User
Package com.infowarelab.test;
Import Java.io.BufferedInputStream;
Import Java.io.BufferedReader;
Import Java.io.ByteArrayOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStreamWriter;
Import Java.net.URI;
Import Java.net.URL;

Import java.net.URLConnection;
Import org.apache.commons.httpclient.HttpClient;
Import Org.apache.commons.httpclient.HttpStatus;

Import Org.apache.commons.httpclient.methods.PostMethod; /** * Test calls some meeting third party interface * @author Jack.song/public class Testmeetinginterface {/** * @param args/Public
		static void Main (string[] args) {String url = ' Http://192.168.0.68/integration/xml ';
		Testmeetinginterface TMI = new Testmeetinginterface ();
		
		System.out.println (tmi.post (URL, "Listsummarymeeting.xml")); /*//determine if the current system supports Java AWT Desktop extension if (java.awt.Desktop.isDesktopSupported ()) {try {URI pat h = Tmi.getclass (). GetResource ("/listsummarymeetinG.xml "). Touri ();
                SYSTEM.OUT.PRINTLN (path); 
                Create a URI instance//Java.net.URI URI = java.net.URI.create (path);
                Gets the current system desktop extension java.awt.Desktop DP = Java.awt.Desktop.getDesktop (); 
                    Determine if the system desktop supports functionality to be performed if (dp.issupported (Java.awt.Desktop.Action.BROWSE)) {//Get system default browser Open link    
                Dp.browse (path);
            } catch (Exception e) {e.printstacktrace ();  }*/}/** * Send XML data request to server side * @param URL XML Request data address * @param xmlstring The sent XML data stream * @return null send failed, otherwise return response content */public string post (string url,string xmlfilename) {/   
        /Close System.setproperty ("Org.apache.commons.logging.Log", "Org.apache.commons.logging.impl.SimpleLog");   
        System.setproperty ("Org.apache.commons.logging.simplelog.showdatetime", "true"); System.setpropErty ("Org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "stdout");  
        Create HttpClient Tool Object HttpClient client = new HttpClient ();  
        Create Post request method Postmethod MyPost = new Postmethod (URL);
        Set the request Timeout client.setconnectiontimeout (300*1000);  
        String responsestring = null;
        	try{//Set Request header type Mypost.setrequestheader ("Content-type", "Text/xml");
        	
        	Mypost.setrequestheader ("CharSet", "Utf-8");
        	
        	Set the request body, which is the XML text content, note: Here are two ways to get the XML content string directly, one is to read the XML file in stream form/mypost.setrequestbody (xmlstring);
        	InputStream Body=this.getclass (). getResourceAsStream ("/" +xmlfilename);
Mypost.setrequestbody (body);  
            Mypost.setrequestentity (New Stringrequestentity (xmlstring, "Text/xml", "Utf-8"));  
            int statusCode = Client.executemethod (mypost); if (StatusCode = = HTTPSTATUS.SC_OK) {bufferedinputstream bis = new Bufferedinputstream (Mypost.getresponsebodyasstream ());  
                byte[] bytes = new byte[1024];  
                Bytearrayoutputstream BOS = new Bytearrayoutputstream ();  
                int count = 0;  
                while (count = Bis.read (bytes))!=-1) {bos.write (bytes, 0, count);  
                } byte[] Strbyte = Bos.tobytearray ();  
                responsestring = new String (strbyte,0,strbyte.length, "utf-8");  
                Bos.close ();  
            Bis.close ();  
        }}catch (Exception e) {e.printstacktrace ();  
        } mypost.releaseconnection ();  
    return responsestring;
            /** * Request with traditional URI class * @param urlstr */public void Testpost (String urlstr) {try {
            URL url = new URL (urlstr);
            URLConnection con = url.openconnection ();
            Con.setdooutput (TRUE); Con.setrequestproperty ("Pragma:", "No-cache");
            Con.setrequestproperty ("Cache-control", "No-cache");

            Con.setrequestproperty ("Content-type", "Text/xml");    
            OutputStreamWriter out = new OutputStreamWriter (Con.getoutputstream ());
            String xmlinfo = Getxmlinfo ();
System.out.println ("urlstr=" + urlstr);
            System.out.println ("xmlinfo=" + xmlinfo);
            Out.write (New String (Xmlinfo.getbytes ("UTF-8")));
            Out.flush ();
            Out.close ();
            BufferedReader br = new BufferedReader (con. getInputStream ()) (new InputStreamReader);
            String line = "";
            For line = Br.readline (), line!= null, line = Br.readline ()) {System.out.println (line);
        } catch (Exception e) {e.printstacktrace ();
        } private String Getxmlinfo () {StringBuilder sb = new StringBuilder (); Sb.append ("<?xml version= ' 1.0 ' encoding= ' UTF-8 '"? > ");
        Sb.append ("<Message>");
        Sb.append (" 

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.