Package Com.liuc;import Java.io.bytearrayoutputstream;import Java.io.ioexception;import java.io.InputStream;import Java.io.unsupportedencodingexception;import Java.util.arraylist;import Java.util.list;import Java.util.Map;import Org.apache.http.httpresponse;import Org.apache.http.namevaluepair;import Org.apache.http.client.clientprotocolexception;import Org.apache.http.client.httpclient;import Org.apache.http.client.config.requestconfig;import Org.apache.http.client.entity.urlencodedformentity;import Org.apache.http.client.methods.httppost;import Org.apache.http.impl.client.httpclients;import Org.apache.http.message.basicnamevaluepair;public class Httppostutil {public static String Sendhttppost (map< String, string> map,string encode,string path) {list<namevaluepair> list=new arraylist<namevaluepair> ( if (Map!=null&&!map.isempty ()) {for (map.entry<string, string> entry:map.entrySet ()) {List.add (new Basicnamevaluepair (Entry.getkey (), Entry.getvalue ()));}} TryThe {///implementation encapsulates the parameters into the form, i.e. request weight urlencodedformentity entity=new urlencodedformentity (List,encode);//submit data using Post HttpPost Httppost=new httppost (path); httppost.setentity (entity); Requestconfig requestconfig = Requestconfig.custom (). SetSocketTimeout (.). Setconnecttimeout (. build);// Setup request and transfer timeout after 4.3 httppost.setconfig (requestconfig);//Perform POST request//3.x this is//httpclient httpclient=new Defaulthttpclient ();//4.x is such a httpClient httpClient = Httpclients.createdefault (); HttpResponse Httpresponse=httpclient.execute (HttpPost); if (Httpresponse.getstatusline (). GetStatusCode () ==200) { Return Getstringforinputstream (Httpresponse.getentity (). GetContent (), encode);}} catch (Unsupportedencodingexception e) {e.printstacktrace ();} catch (Clientprotocolexception e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} return path;} private static String Getstringforinputstream (InputStream inputstream,string encode) {Bytearrayoutputstream stream= New Bytearrayoutputstream (); byte[] Data=new byte[1024];int len=0; String result= "", try {if (InputStream! = null) {while (len = inputstream.read (data))! =-1) {stream.write (data, 0, Len);} Result=new String (Stream.tobytearray (), encode);}} catch (Exception e) {}return result;}}
The
differs for each version of the notation. can refer to: http://www.open-open.com/lib/view/open1383751765321.html