Use Java Native API to compile a tool class for sending http_post requests

Source: Internet
Author: User
Tags response code
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 // ================================================ ===================================================//
Related Article

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.