Java post data to third-party URLs

Source: Internet
Author: User

I used to sync the data to the server
1. Send a TXT text using FTP and then disassemble it with PHP.

2. Using a direct link database
The quality of the scheme depends on 1. Whether the database can be directly linked (security is also a problem) 2. Link efficiency of the database. Outside links, always less fast than local links.

3. Talk about the third approach today, JAVA post data to the server

Send a POST request

String sr=httprequest.sendpost ("http://localhost:6144/Home/RequestPostString", "key=123&v=456");
/** * Request to send a POST method to the specified URL * * @param URL * Send request URL * @param param * Request parameter, request parameter should be Name1=value1&amp ; a form of name2=value2. * @return The response result of the remote resource represented by */public static string sendpost (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);//The Send POST request must be set to the following two lines Conn.setdooutput (true), conn.setdoinput (TRUE);//get the output stream for the URLConnection object out = new PrintWriter (Conn.getoutputstream ());//Send request parameter out.print (param);//Flush output stream buffer Out.flush ();// Defines the BufferedReader input stream to read the response of the URL in = new BufferedReader (New InputStreamReader (Conn.getinputstream ())); 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;}

Java post data to third-party URLs

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.