Android sends post requests for HTTP protocol

Source: Internet
Author: User

/** * The way the POST request is used * * @param username * @param password * @return NULL indicates a problem with the evaluated path, text returns the requested data */public static String Pos Trequest (string Username, string password) {try {string path = "http://172.22.64.156:8080/0001AndroidWebService/ Loginservlet "; URL url = new URL (path); HttpURLConnection conn = (httpurlconnection) url.openconnection (); Conn.setreadtimeout (+) Conn.setrequestmethod (" POST "),//username=donghongyu&&password=123//prepare the data to be transferred string" username= "+ urlencoder.encode (username) + "&password=" + urlencoder.encode (password);//sets the type of the requested content Conn.setrequestproperty ("Content-type", "application/ X-www-form-urlencoded "); Conn.setrequestproperty (" Content-length ", data.length () +" ");// Open the Write permissions to the server Conn.setdefaultusecaches (TRUE);//get the output stream of the HTTP connection outputstream OS = Conn.getoutputstream ();// Writes Data Os.write (Data.getbytes ()) to the server, int code = Conn.getresponsecode (), if (code = = 200) {//Request succeeds InputStream is = CONN.GETINP Utstream (); String Text = Streamutil.readstream (is); return text;} else {//request failed return null;}} catch (Exception e) {e.printstacktrace ();} return null;}

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.