请求方式一:
HTTP请求:
JSONObject object=new JSONObject();
object.put(“method”, “xxxxxxx”);
HttpRequest req=new HttpRequest(“http://xxxxxx”);
req.request(object);
HTTP调用类:
public class HttpRequest {
private static final Logger log = LoggerFactory.getLogger(HttpRequest.class);
    private final String url;
     private OkHttpClient httpClient;
     private ObjectMapper mapper = new ObjectMapper();
    public HttpRequest(String url) {
         this(url, new OkHttpClient.Builder().addInterceptor(new Interceptor() {
             @Override
             public Response intercept(Chain chain) throws IOException {
                 Request request = chain.request().newBuilder().addHeader(“Connection”, “close”).build();
                 return chain.proceed(request);
             }
         }).readTimeout(30, TimeUnit.SECONDS).build());
     }
    public HttpRequest(String url, OkHttpClient client) {
         this.url = url;
         log.info(“Http URL: ” + url);
         httpClient = client;
     }
    public String request(Object content) throws IOException {
         String sendTxt = mapper.writeValueAsString(content);
         RequestBody body = RequestBody.create(MediaType.parse(“application/json; charset=utf-8”), sendTxt);
         Request req = new Request.Builder().url(url).post(body).build();
         log.info(“=> Request: ” + content);
         try (Response rsp = httpClient.newCall(req).execute()) {
             if (!rsp.isSuccessful()) {
                 return rsp.message();
             }
             ResponseBody rspBody = rsp.body();
             if (rspBody != null) {
                 String json = rspBody.string();
                 log.info(“<= Response: ” + json);
                 return json;
             } else {
                 return “EMPTY RESPONSE”;
             }
        }
     }
 }
请求方式二:
实体类:
public class HaiKangUpdate {
     private String name;
     private int freenum;
     public String getName() {
         return name;
     }
     public void setName(String name) {
         this.name = name;
     }
     public int getFreenum() {
         return freenum;
     }
     public void setFreenum(int freenum) {
         this.freenum = freenum;
     }
 }
HTTP请求:
List<HaiKangUpdate> listUpdate=new ArrayList<HaiKangUpdate>();
HaiKangUpdate haiKangUpdateB=new HaiKangUpdate();
 haiKangUpdateB.setName(“苏州”);
 haiKangUpdateB.setFreenum(10000);
 listUpdate.add(haiKangUpdateB);
String json = JSON.toJSONString(listUpdate);
 String response=HttpPostUtils.httpPost(“http://xxxxxx”, json);
HTTP调用类:
import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.PrintWriter;
 import java.net.HttpURLConnection;
 import java.net.URL;
import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
public class HttpPostUtils {
     private final static Logger logger = LoggerFactory.getLogger(HttpPostUtils.class);
     
     public static String httpPost( String url, String param ) {
         logger.info(“请求地址:”+url);
         logger.info(“请求参数:”+param);
         PrintWriter out = null;
         InputStream is = null;
         BufferedReader br = null;
         String result = “”;
         HttpURLConnection conn = null;
         StringBuffer strBuffer = new StringBuffer();
         try {
             URL realUrl = new URL(url);
             conn = (HttpURLConnection) realUrl.openConnection();
             // 设置通用的请求属性
             conn.setRequestMethod( “POST”);
             conn.setConnectTimeout(20000);
             conn.setReadTimeout(300000);
             conn.setRequestProperty(“Charset”, “UTF-8”);
  
             // 传输数据为json,如果为其他格式可以进行修改
             conn.setRequestProperty( “Content-Type”, “application/json”);
             conn.setRequestProperty( “Content-Encoding”, “utf-8”);
             // 发送POST请求必须设置如下两行
             conn.setDoOutput( true);
             conn.setDoInput( true);
             conn.setUseCaches( false);
             // 获取URLConnection对象对应的输出流
             out = new PrintWriter(conn.getOutputStream());
             // 发送请求参数
             out.print(param);
             // flush输出流的缓冲
             out.flush();
             is = conn.getInputStream();
             br = new BufferedReader( new InputStreamReader(is));
             String line = null;
             while ((line=br.readLine())!= null) {
                 strBuffer.append(line);
             }
             result = strBuffer.toString();
         } catch (Exception e) {
             System. out.println( “发送 POST 请求出现异常!” + e);
             e.printStackTrace();
         }
         // 使用finally块来关闭输出流、输入流
         finally {
             try {
                 if (out != null) {
                     out.close();
                 }
                 if (br != null) {
                     br.close();
                 }
                 if (conn!= null) {
                     conn.disconnect();
                 }
             } catch (IOException ex) {
                 ex.printStackTrace();
             }
         }
         return result;
     }
 }
请求方式三:
HTTP请求:
JSONObject params = new JSONObject();
 params.put(“list”, jsonArray);          
params.put(“list”, jsonArray);
//请求接口
 logger.info(“参数结果:”+params.toJSONString());
 String string = ParkInfoHttp.sendPost(requestUrl, params.toJSONString());
 ogger.info(“返回结果:”+string);
HTTP调用类:
import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
 import org.apache.commons.httpclient.methods.PostMethod;
 import org.apache.commons.httpclient.methods.RequestEntity;
public class ParkInfoHttp {
     
     /**
      * 发送post请求
      * 
      * @param params
      *            参数
      * @param requestUrl
      *            请求地址
      * @return 返回结果
      * @throws IOException
      */
     public static String sendPost(String requestUrl, String params) throws IOException {
        byte[] requestBytes = params.getBytes(“utf-8”); // 将参数转为二进制流
         HttpClient httpClient = new HttpClient();// 客户端实例化
         PostMethod postMethod = new PostMethod(requestUrl);
         // 设置请求头 Content-Type
         postMethod.setRequestHeader(“Content-Type”, “application/json”);
         InputStream inputStream = new ByteArrayInputStream(requestBytes, 0, requestBytes.length);
         RequestEntity requestEntity = new InputStreamRequestEntity(inputStream, requestBytes.length,
                 “application/json; charset=utf-8”); // 请求体
         postMethod.setRequestEntity(requestEntity);
         httpClient.executeMethod(postMethod);// 执行请求
         InputStream soapResponseStream = postMethod.getResponseBodyAsStream();// 获取返回的流
         byte[] datas = null;
         try {
             datas = readInputStream(soapResponseStream);// 从输入流中读取数据
         } catch (Exception e) {
             e.printStackTrace();
         }
         String result = new String(datas, “UTF-8”);// 将二进制流转为String
         return result;
    }
     
     /**
      * 发送post请求
      * 
      * @param params
      *            参数
      * @param requestUrl
      *            请求地址
      * @param authorization
      *            授权书
      * @return 返回结果
      * @throws IOException
      */
     public static String sendPost(String params, String requestUrl, String authorization) throws IOException {
        byte[] requestBytes = params.getBytes(“utf-8”); // 将参数转为二进制流
         HttpClient httpClient = new HttpClient();// 客户端实例化
         PostMethod postMethod = new PostMethod(requestUrl);
         // 设置请求头Authorization
         if (!isNullOrEmpty(authorization)) {
             postMethod.setRequestHeader(“Authorization”, authorization);
         }
         // 设置请求头 Content-Type
         postMethod.setRequestHeader(“Content-Type”, “application/json”);
         InputStream inputStream = new ByteArrayInputStream(requestBytes, 0, requestBytes.length);
         RequestEntity requestEntity = new InputStreamRequestEntity(inputStream, requestBytes.length,
                 “application/json; charset=utf-8”); // 请求体
         postMethod.setRequestEntity(requestEntity);
         httpClient.executeMethod(postMethod);// 执行请求
         InputStream soapResponseStream = postMethod.getResponseBodyAsStream();// 获取返回的流
         byte[] datas = null;
         try {
             datas = readInputStream(soapResponseStream);// 从输入流中读取数据
         } catch (Exception e) {
             e.printStackTrace();
         }
         String result = new String(datas, “UTF-8”);// 将二进制流转为String
         return result;
    }
     
     /**
      * 从输入流中读取数据
      * 
      * @param inStream
      * @return
      * @throws Exception
      */
     public static byte[] readInputStream(InputStream inStream) throws Exception {
         ByteArrayOutputStream outStream = new ByteArrayOutputStream();
         byte[] buffer = new byte[1024];
         int len = 0;
         while ((len = inStream.read(buffer)) != -1) {
             outStream.write(buffer, 0, len);
         }
         byte[] data = outStream.toByteArray();
         outStream.close();
         inStream.close();
         return data;
     }
     
     // String array
     public static boolean isNullOrEmpty(String… values) {
         for (String value : values) {
             if (isNullOrEmpty(value))
                 return true;
         }
         return false;
     }
    // String
     public static boolean isNullOrEmpty(String value) {
         if (null == value || “”.equals(value.trim())) {
             return true;
         }
         return false;
     }
 }
接收方式一:
@RequestMapping(“/upload”)
 @ResponseBody    
public void upload(HttpServletRequest request) throws IOException{
         HttpSession session=request.getSession(false);
         if (session== null) {
             logger.error(“session值:”+session);
             return this.customResult(1005, “session取值为空!”);
         }
         Enumeration<String> names=session.getAttributeNames();
         Map<String,Object> map=null;
         boolean flag=false;
         while(names.hasMoreElements()){
             if(flag){
                 break;
             }
             String name=names.nextElement().toString();
             String req=session.getAttribute(name).toString();
            //反序列化数据,获取数据
             CommonParam param=JSONObject.parseObject(req, CommonParam.class);
             String method=param.getMethod();
         }
     }
接收方式二:
    @ResponseBody
     @RequestMapping(value = “/xxxxxxx”)
     public void cityParkingList(@RequestBody Map<String, Object> map) {
         try {
             if(map.get(“parkcode”)==null||map.get(“parkcode”).equals(“”)) {
                 logger.info(“parkcode参数为空”);
             }
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
接收方式三:
    @RequestMapping(“/upload”)
     @ResponseBody
     public void upload(@RequestBody String params) throws IOException{
logger.error(“params值:”+params);
if(params!=null) {
                //反序列化数据
                 CommonParam param=JSONObject.parseObject(params, CommonParam.class);
String method=param.getMethod();
}
}
接收方式四:
    @RequestMapping(value = “/xxxxxx”)
     public void cityParkingList(HttpServletRequest request) {
try {
            BufferedReader in = new BufferedReader(new                 InputStreamReader(request.getInputStream(),Charset.forName(“UTF-8”)));
             StringBuilder sb = new StringBuilder();
             String line = null;
             while ((line = in.readLine()) != null) {
                 sb.append(line);
             }
             String jstr=sb.toString();
             logger.info(“接收到字符串为:”+jstr);
        }catch (Exception e) {
             logger.info(“插入数据失败,程序异常”);
             e.printStackTrace();
         }
}