使用HttpClient 依赖jar包
1:commons-httpclient-3.0.jar
2:commons-logging-1.1.1.jar
3:commons-codec-1.6.jar
本地调用测试===>>>>>>>>>>>>
package com.me.cn.siteTrans.test; import java.util.HashMap; import java.util.Map; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.PostMethod; import com.google.gson.Gson; public class SiteTran { public static void main(String[] args) { String result = httpClientTest(); System.out.println(result); } public static String httpClientTest(){ HttpClient client = new HttpClient() ; PostMethod method = null;//post 方式 get 方式 GetMethod gMethod String result = "" ; try { method = new PostMethod("http://localhost:8666/test/siteTrans/returnJson.do") ; method.setParameter("param","test");//设置参数 client.executeMethod(method); if(method.getStatusCode() == HttpStatus.SC_OK){//响应成功 result = method.getResponseBodyAsString();//调用返回结果 }else{//不成功组装结果 Map<String , Object >map = new HashMap<String , Object>(); Gson gson = new Gson() ; map.put("success", false); result = gson.toJson(map); } } catch (Exception e) { e.printStackTrace(); }finally{ method.releaseConnection(); } return result ; } }
模拟第三方接口===>>>>>>>>>>>>
package com.cn.me.siteTrans.interface; import java.util.HashMap; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.google.gson.Gson; @Controller @RequestMapping("/siteTrans") @SuppressWarnings("all") public class SiteTransCtrl { @RequestMapping("/returnJson") @ResponseBody public Map returnJson(String param){ Map<String , Object> map = new HashMap<String , Object>() ; try { System.out.println(param); map.put("success", true) ; map.put("flag", true); } catch (Exception e) { e.printStackTrace(); } return map; } }