Http simulate Form form submit POST request

Source: Internet
Author: User
private static Logger log = Loggerfactory.getlogger (Httppostutil.class); public static string Reqxinyi (string url, string appkey,string data,string sign) throws ioexception{Objectmapper Ma
    Pper = new Objectmapper ();
    String param= null;
    String B = null; try {param = "appkey=" + urlencoder.encode (Appkey, "UTF-8") + "&data=" +urlencoder.encode (data, "UTF-8") + "&si
        gn= "+urlencoder.encode (sign," UTF-8 ");
    Log.info ("Stitching request parameter param:" +param);
        catch (Unsupportedencodingexception e) {log.error ("stitching request parameter Error");
    E.printstacktrace ();
        } log.info ("Start sending request");
        URL url = new URL (URLs);
        URLConnection connection = Url.openconnection ();
        HttpURLConnection httpconn = (httpurlconnection) connection;
        Set the request method to post Httpconn.setrequestmethod ("POST");
        Sets whether the output Httpconn.setdooutput (true) is required;
        Sets whether you want to allow input httpconn.setdoinput (true); Set request Properties Httpconn.setrequestproPerty ("Content-length", String.valueof (Param.getbytes (). Length));
        Httpconn.setrequestproperty ("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
        Write data to server outputstream out = Httpconn.getoutputstream ();
        Out.write (Param.getbytes ("UTF-8"));
        Out.close ();
        The data returned by the receiving server InputStream in = Httpconn.getinputstream ();
        Bytearrayoutputstream resp = new Bytearrayoutputstream ();
        byte[] tempbytes = new byte[100];
        int byteread = 0;
        while ((Byteread = In.read (tempbytes))!=-1) {resp.write (tempbytes, 0, Byteread);
        } B = New String (Resp.tobytearray ());
        Log.info ("Call interface return: {}", b);
return b;
 }

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.