public static string Sendpost (string url, string param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realurl = new URL (URL);
Opening and linking between URLs
URLConnection conn = Realurl.openconnection ();
To set common request properties
Conn.setrequestproperty ("Accept", "*/*");
Conn.setrequestproperty ("Connection", "keep-alive");
Conn.setrequestproperty ("User-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ");
To send a POST request, you must set the following two lines
Conn.setdooutput (TRUE);
Conn.setdoinput (TRUE);
Gets the output stream corresponding to the URLConnection object
out = new PrintWriter (Conn.getoutputstream ());
Send Request parameters
Out.print (param);
Buffer for flush output stream
Out.flush ();
Defines the response of the BufferedReader input stream to read 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 backend Call Interface post parameter instance