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