Springmvc template and httpclient post submission

Source: Internet
Author: User

If the interface of the server is SPRINGMVC, the client, in addition to the Resttemplate request provided by SPRINGMVC, is as follows:

 Public classrestclient {Private StaticLogger Logger = Logger.getlogger (Restclient.class); @SuppressWarnings ({"Rawtypes","Unchecked"}) Public StaticObjectPost(String URL, map<string, object> message) {Object result =NULL;Try{Resttemplate rest =NewResttemplate (); multivaluemap<string, object> param =NewLinkedmultivaluemap (); for(Entry<string, Object> entry:message.entrySet ())            {Param.add (Entry.getkey (), Entry.getvalue ());        } result = Rest.postforobject (URL, param, string.class); }Catch(Exception e) {Logger.error ("Send Message exception occurred"+E); }returnResult }}

You can also send requests using HttpClient, as follows:

Packagecom. CKDH. Web. Test;Import Java. IO. BufferedReader;Import Java. IO. InputStream;Import Java. IO. InputStreamReader;import org. Apache. HTTP. Httpentity;import org. Apache. HTTP. HttpResponse;import org. Apache. HTTP. Client. HttpClient;import org. Apache. HTTP. Client. Methods. HttpPost;import org. Apache. HTTP. Entity. Stringentity;import org. Apache. HTTP. Impl. Client. Defaulthttpclient;public class Downloadresourcestest {public static void main (string[] args) {String URL ="Http://localhost:8080/xxx-web/xxx.mvc?apikey=1";HttpClient client = new Defaulthttpclient (); HttpPost HttpPost = new HttpPost (URL); InputStream is = null;InputStreamReader ISR = null;BufferedReader br = NULL;try {HttpPost. AddHeader("City","010");HttpPost. AddHeader("Version","2");httpentity entity = new Stringentity ("<infos><info spid=\" 188\ "hash=\" 4a0fd9704eb1432892cbc19742811b63\ ">"+"</info><info spid=\" 1601\ "hash=\" 4e7b8894b8bc4d4eac22dffd85f28a68\ "></info></infos" > ");HttpPost. Setentity(entity);HttpResponse response = Client. Execute(HttpPost);System. out. println(Response. Getstatusline());is = response. GetEntity(). GetContent();ISR = new InputStreamReader (IS,"UTF-8");br = new BufferedReader (ISR);StringBuffer buf = new StringBuffer ();String Line;while (null! = (line = BR. ReadLine())) {BUF. Append(line). Append("\ n");} System. out. println(buf. toString());} catch (Exception e) {E. Printstacktrace();}    }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Springmvc template and httpclient post submission

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.