/**
*java client sends HTTP request
*/
Package com.xx.httptest;/** * Created by Yq on 16/6/27. */import Java.io.ioexception;import Java.net.urlencoder;import Org.apache.commons.httpclient.*;import Org.apache.commons.httpclient.methods.getmethod;import Org.apache.commons.httpclient.methods.postmethod;import Org.apache.commons.httpclient.methods.requestentity;import Org.apache.commons.httpclient.methods.stringrequestentity;import Org.apache.http.params.CoreConnectionPNames; Import Org.json.jsonexception;import Org.json.jsonobject;public class Httpclienttest {public static void main (string[] args) throws Exception {String url = "Http://localhost:8030/workflowapi/workflowextend"; String host = "www.127.0.0.1"; String param = "startcity=" + urlencoder.encode ("Hangzhou", "utf-8") + "&lastcity=&thedate=&userid="; HttpClient HttpClient = new HttpClient (); Httpclient.getparams (). Setparameter (Coreconnectionpnames.connection_timeout, 60000); Httpclient.gethostconfiguration (). SethosT (host, 8030, "http"); HttpMethod method = GetMethod (URL, param); HttpMethod method = postMethod1 (URL); System.out.println ("Print send status---->"); System.out.println (Method.getstatuscode ()); int sendstatus = 0; try {sendstatus = Httpclient.executemethod (method); System.out.println ("Print send Status"); System.out.println (Sendstatus); System.out.println ("dddddddd"); String response = method.getresponsebodyasstring (); SYSTEM.OUT.PRINTLN (response); } catch (Exception e) {e.printstacktrace (); } finally {method.releaseconnection (); }//string response = new String (method.getresponsebodyasstring (). GetBytes ("iso-8859-1")); & nbsp; //system.out.println (response); } private static HttpMethod GetMethod (String url,string param) throws IOException { GetMethod get = new GetMethod (URL + "?" + param); Get.releaseconnection (); return get; }/* Send form form parameter */private static HttpMethod Postmethod (String url) throws IOException {P Ostmethod post = new Postmethod (URL); Post.setrequestheader ("Content-type", "application/x-www-form-urlencoded;charset=utf-8"); namevaluepair[] param = {new Namevaluepair ("Startcity", "Hangzhou"), New Namevaluepair ("Lastcity", "Shenyang"), New Namevaluepair ("UserID", ""), New Namevaluepair ("Thedate", "")}; Post.setrequestbody (param); Post.releaseconnection (); return post; }/* Send JSON data */private static HttpMethod postMethod1 (String url) throws ioexception{Postmethod Post = new Postmethod (URL); Post.setrequestheader ("Content-type", "application/x-www-form-urlencoded;charset=utf-8"); Post.setrequestheAder ("Content-type", "Application/json"); Jsonobject jsonobject = new Jsonobject (); Jsonobject jsonObject2 = new Jsonobject (); try {jsonobject.put ("aaaaa", "ddddd"); Jsonobject.put ("bbbbb", "ddddd2"); Jsonobject.put ("CCCCC", "ddddd3"); Jsonobject2.put ("55555", jsonobject); Jsonobject2.put ("66666", "testtest"); } catch (Jsonexception e) {e.printstacktrace (); } requestentity requestentity = new Stringrequestentity (jsonobject2.tostring (), "Text/xml", "UTF-8"); Post.setrequestentity (requestentity); Post.releaseconnection (); return post; }}
Go Service side
Func (this *workflowapicontroller) Workflowextend () {fmt. PRINTLN ("Print post data") FMT. Println (this. Ctx.Request.Body) Body, _: = Ioutil. ReadAll (this. Ctx.Request.Body) var dat map[string]interface{}if err: = json. Unmarshal (body, &dat); Err = = Nil {fmt. PRINTLN ("Print map----->", DAT)}else {fmt. PRINTLN ("Print error----->", err. Error ())}fmt. Println (this. GetString ("startcity")) Formdata: = this. GetString ("Formdata") fmt. PRINTLN ("Print receive data------>>>>>", formdata) Formdatamap: = Map[string]interface{}{}json. Unmarshal ([]byte (Formdata), &formdatamap)//fmt. PRINTLN ("Call HTTP interface print--->>,", formdatamap) Resmap: = map[string]interface{}{}resmap["code"] = "1" this. data["json"] = Resmapthis.servejson ()}
Java httpclient send JSON request, go server receive