Package COM. jadyer. util; import Java. io. bytearrayoutputstream; import Java. io. inputstream; import Java. io. outputstream; import Java. io. unsupportedencodingexception; import java.net. httpurlconnection; import java.net. URL; import java.net. urlencoder; import Java. util. hashmap; import Java. util. list; import Java. util. map;/*** use Java Native API to compile the tool class for sending http_post requests * @ see ------------------------------------------------ ---------------------------------------- * @ See is similar to another httpclientutil tool class * @ see address for http://blog.csdn.net/jadyer/article/details/8087960 * @ see users * @ see the beauty of this method is: when the server returns 500, it directly throws exceptions similar to the following * @ see Java. io. ioexception: server returned HTTP response code: 500 for URL: http: // xxxx/xxxx * @ see. The reason is simple: here, the base provided by Sun is used. In the HTTP Protocol framework * @ see references * @ see personally recommended use socket to send HTTP Request * @ See example see http://blog.csdn.net/jadyer/article/details/8788272 * @ see references * @ create Mar 4, 2013 2:47:57 pm * @ editor APR 4, 2013 10:02:18 am * @ author Xuan Yu
The following is the test code.
Public static void main (string [] ARGs) {Map <string, string> Params = new hashmap <string, string> (); Params. put ("merno", "301100100001630"); Params. put ("signtype", "MD5"); Params. put ("merbindagrno", "00003018007000006450000013866742"); Params. put ("interfaceversion", "1.0.0.0"); Params. put ("amount", "1000"); Params. put ("orderdate", "20120823"); Params. put ("orderno", "udp1208230917531231111"); Params. put ("merreqtime", "20120823091802"); Params. put ("goodsdesc", "Pay for the number"); Params. put ("goodsname", "China Unicom recharge"); Params. put ("useridemark", "3"); Params. put ("bankagrmode", "9"); Params. put ("signmsg", "timeout"); Map <string, string> respmap = sendpostrequest ("http: // 127.0.0.1/TRA/bind/payment.htm", Params, "UTF-8"); system. out. println ("========================================== ========================================================== "); system. out. println ("htt response code as follows"); system. out. println (respmap. get ("respcode"); system. out. println ("========================================== ========================================================== "); system. out. println ("The htt response body is as follows"); system. out. println (respmap. get ("respbody"); system. out. println ("========================================== ========================================================== "); system. out. println ("complete HTTP response packet is as follows"); system. out. println (respmap. get ("respmsg"); system. out. println ("========================================== ========================================================== ");}
Below is the console output
// The console outputs the following: // =================================================== ========================================================== ====/// htt response code: // 200 /// ================================= ========================================================== ==============/// htt response body: // interfaceversion = 1.0.0.0 // merreqtime = // payjournl = // amount = // orderno = udp1208230917531231111 // ordertime = 20120823 // paybankno = // bankaccno = // bankaccname = // transret = illegal_merchant_no // resultdis = the signature key of the merchant cannot be queried. // acceptdate =/ /accepttime = // signtype = MD5 // signmsg = 29a312ab08b7273d7500a960c8c86b73 // ============== ========================================================== ==============/// the complete HTTP response packet is as follows // HTTP/1.1 200 OK // Content-Length: 289 // Content-Type: text/html; charset = GBK // interfaceversion = 1.0.0.0 // merreqtime = // payjournl = // amount = // orderno = udp1208230917531231111 // ordertime = 20120823 // paybankno = // bankaccno // bankaccname = // transret = illegal_merchant_no // resultdis = the signature key of the merchant fails to be queried. // acceptdate = // accepttime = // signtype = MD5 // signmsg = Signature // ================================================ ===================================================//