Springmvc template and httpclient post submission

Source: Internet
Author: User

The interface of the server if it is the SPRINGMVC client in addition to the Resttemplate request provided with SPRINGMVC is as follows


public class Restclient {

private static Logger Logger = Logger.getlogger (Restclient.class);

@SuppressWarnings ({rawtypes, unchecked})

public static Object post (String URL, map<string, object= "" > Message) {

Object result = null;

try {

Resttemplate rest = new Resttemplate ();

Multivaluemap<string, object= "" > param = new Linkedmultivaluemap ();

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 +e);

}

return result;

}

}


Import java.io.UnsupportedEncodingException;

Import Org.springframework.util.LinkedMultiValueMap;

Import Org.springframework.util.MultiValueMap;

Import Org.springframework.web.client.RestTemplate;

Import Test. Threedes;

public class Testpost {

public static void Main (string[] args) {

//spring Way

resttemplate rest = new Resttemplate ();//Create Org.springframework.web.client.RestTemplate

multivaluemap<string,object> param = new linkedmultivaluemap<string,object> ();//Create parameter map Org.springframework.util.LinkedMultiValueMap

Map Set Value

Param.add ("id", "1");

Param.add ("Carbonorderno", "M201702161055");

Param.add ("name", "King Smoke");

Param.add ("Usertypes", "other");

Param.add ("tel", "13020232323");

Param.add ("Certificateno", "99021023456");

Param.add ("Money", "2");

Param.add ("Donationways", "Cashpay");

String result = Rest.postforobject ("Http://localhost:8080/trip/bD/send?", Param, string.class);//postforobject Post mode

SYSTEM.OUT.PRINTLN (result);

}

}


You can also send requests with HttpClient as follows:

Package com.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 hash= "a0fd9704eb1432892cbc19742811b63" spid= ">" +

</info><info hash= "E7b8894b8bc4d4eac22dffd85f28a68" spid= "P1" ></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 ();

}

System.out.println (Buf.tostring ());

} catch (Exception e) {

E.printstacktrace ();

}

}

}


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.