JAVA background call interface and get return value __java

Source: Internet
Author: User
Java background implementations use URLs to call someone else's interface and get the value returned by the interface
Parameter jsonstr is content, path interface URL address public string sendpost (string jsonstr, String path) throws IOException {byte[] data = jsons
		Tr.getbytes ();
		Java.net.URL URL = new Java.net.URL (path);
		Java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openconnection ();
		Conn.setrequestmethod ("POST"); Conn.setconnecttimeout (5 * 1000);/Set connection timeout of 5 seconds conn.setreadtimeout (20 * 1000);/set read timeout of 20 seconds//Use URL connection for output, will DoO

		The UTPUT flag is set to True Conn.setdooutput (true);
		Conn.setrequestproperty ("Content-type", "text/xml;charset=utf-8");
		Conn.setrequestproperty ("content-encoding", "gzip");
		Conn.setrequestproperty ("Content-length", String.valueof (Data.length));
		OutputStream OutStream = Conn.getoutputstream ();//returns the output stream Outstream.write (data) written to this connection;  Outstream.close ()//close stream String msg = ""//Save response information after calling HTTP service//If the request response code is 200, the success if (conn.getresponsecode () = 200) {//HTTP server-side return encoding is UTF-8, it must be set to UTF-8, keep coding unified, otherwise will appear Chinese garbled bufferedreader in = new BufferedReader (New InputstreamreAder (Conn.getinputstream (), "UTF-8"));
			msg = In.readline ();
		In.close ();
	Conn.disconnect ();//Disconnect return msg; }


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.