Java background POST request carries parameters remote operation call interface

Source: Internet
Author: User

Tag:put    get     Call interface    tac   com    ble    request    use    call    

Package Com.huayu.tizong.matchteam.util;import Java.io.bufferedreader;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.io.outputstreamwriter;import Java.io.printwriter;import Java.net.malformedurlexception;import java.net.url;import java.net.urlconnection;/** * Request Interface Data * * @author Administrator * */public class Httpposturl {/** * * @param URL * @param param parameter k=v * @return */public static String s Endpost (string url, string param) {printwriter out = null; BufferedReader in = null; String result = "", try {url realurl = new URL (URL),//Open and URL connection URLConnection conn = Realurl.openconnection ();//set generic request genus Sexual Conn.setrequestproperty ("accept", "*/*"), Conn.setrequestproperty ("Connection", "keep-alive"); Conn.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 6.0; Windows NT 5.1; SV1), Conn.setrequestproperty ("Charsert", "UTF-8");//The Send POST request must be set to the following two lines conn.setdooutput (true); Conn.setdoinput ( true)//gets the output stream corresponding to the URLConnection object out = new PrintWriter (Conn.getoutputstream ());//Send request parameter out.print (param);//Flush output stream buffer Out.flush ();//define BufferedReader input stream to read URL response in = new BufferedReader (new InputStreamReader (Conn.getinputstream (), "Utf-8")); String Line;while (line = In.readline ()) = null) {result + = line;}} catch (Exception e) {System.out.println ("send POST request exception! "+ e); E.printstacktrace ();} Use the finally block to close the output stream, input stream finally {try {if (out! = null) {Out.close ();} if (in = null) {In.close ();}} catch (IOException ex) {ex.printstacktrace ();}} return result;} /** * Data stream POST Request * * @param urlstr * @param xmlinfo */public static string DoPost (String urlstr, String strinfo) {string R Estr = ""; 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"); O Utputstreamwriter out = new OutputStreamWriter (Con.getoutputstream ()); Out.write (New String (Strinfo.getbytes ("Utf-8" )); Out.flush (); Out.close (); BufFeredreader br = new BufferedReader (New InputStreamReader (Con.getinputstream (), "Utf-8")); String line = "", for (line = Br.readline (); Line! = NULL; line = Br.readline ()) {restr + = line;}} catch (Malformedurlexception e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} return restr;}}

  

Java background POST request carries parameters remote operation call interface

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.