[Android] submits data by post

Source: Internet
Author: User

GET: internal implementation is a Way togroup the URL, theHTTP protocol specifies a maximum length of 4kb,ie Browser Restrictions 1kb

The difference between POST and GET is a little bit more than a few messages .

content-type:application/x-www-form-urlencoded

content-length:93

Main content

Just modify a few places in the previous section of the Code:

Call the Setrequestmethod ("POST") method of the httpurlconnection Object

Call the Setrequestproperty () method of the httpurlconnection object to add a few header information

Splicing good content such as string data="Username="+username, invoking the length () of the string Object method, return length, length +"" empty string is converted to string type

Call the Setdooutput (True) method of the httpurlconnection object to allow write data

Call the Getoutputstream () method of the httpurlconnection object to get the outputstream Object

Call the Write (buffer) method of the outputstream object to write data to the server, parameter:buffer is Byte[] Array, call the getBytes () method of the String object to get byte[]

Service

    /*** Post Pass parameters * *@paramusername *@paramPassword *@return     */     Public Staticstring Loginbypost (string username, string password) {string path=Root_path; Try{URL URL=NewURL (path); String Data= "Username=" +username+ "&password=" +password; HttpURLConnection Conn=(HttpURLConnection) url.openconnection (); Conn.setconnecttimeout (5000); //Set Header informationConn.setrequestmethod ("POST"); Conn.setrequestproperty ("Content-type", "application/x-www-form-urlencoded"); Conn.setrequestproperty ("Content-length", data.length () + ""); //Write DataConn.setdooutput (true); OutputStream OS=Conn.getoutputstream ();                        Os.write (Data.getbytes ()); intCode =Conn.getresponsecode (); if(Code = = 200) {InputStream is=Conn.getinputstream (); String Info=Streamtools.readinputstream (IS); returninfo; }        } Catch(Exception e) {e.printstacktrace (); }        return"Request Failed"; }

[Android] submits data by post

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.