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