httpClient请求将数据传递到接口中

    -- from-date 形式的数据也能传 

public static StringBuffer httpSaveFromDate(Map<String, Object> map, String uri, JSONObject jsonObject) {  HttpClient httpClient = new HttpClient();  //设置请求头的编码格式为UTF-8(这部分很重要要不然数据传递到接口中是乱码的)
  httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");  //设置代理模式
   httpClient.getHostConfiguration().setProxy
                            (jsonObject.get("proxyHost").toString(),  //第三方接口ip
                                Integer.parseInt(jsonObject.get("proxyPort").toString())); //接口端口
        httpClient.getParams().setAuthenticationPreemptive(true);
        HttpConnectionManagerParams managerParams = httpClient.getHttpConnectionManager().getParams();
        //设置请求连接超时时间(毫秒值)
        managerParams.setConnectionTimeout(15000);
        //设置读取资源超时时间(毫秒值)
        managerParams.setSoTimeout(35000);
        //定义请求方式为post请求数据
        PostMethod postMethod = new PostMethod(uri);
        String response = null;
        StringBuffer buffer = new StringBuffer();
        try {
            //传入需要填写的请求的参数个数,比如接口要求 传递参数为5,那么new NameValuePair[5]
            NameValuePair[] par = new NameValuePair[Integer.parseInt(jsonObject.get("paramNumber").toString())];
            int index = 0;
            for (String key : map.keySet()) {
                par[index++] = new NameValuePair(key, map.get(key).toString());
            }
            postMethod.setRequestBody(par);
            int code = httpClient.executeMethod(postMethod);
            //判断响应状态是否为200
            if (code != HttpStatus.SC_OK) {
                //抛出异常信息
                throw new IllegalStateException("request error" + postMethod.getStatusLine());
            }
            //定义一个bufferReader
            BufferedReader bufferedReader = null;
            bufferedReader = new BufferedReader(new InputStreamReader(postMethod.getResponseBodyAsStream(),StandardCharsets.UTF_8));         while ((response = bufferedReader.readLine()) != null) {         buffer.append(response);      }   } catch (Exception e) {   throw new IllegalStateException(e.getMessage());   } finally {   //关闭资源链接    postMethod.releaseConnection();   }   return buffer; //获取到的是服务器的响应参数 }

  --- 获取当前时间将当前时间往后延长自定义天数

public static String plusDay(int numberDay){
    Date date = new Date();
    SimpleDateFormat simp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Calendar calendar = Calendar.getInstance();
    // num为增加的天数,可以改变的
    calendar .add(Calendar.DATE, numberDay);
    date = calendar.getTime();    return format.format(date); } //例如:plusDay(5) --返回得到的是第五天的时间

ps:多嘴说下

httpSaveFromDate(Map<String,Object>map,String uri,JsonObject jsonObject) 这个方法的用法 如下:
public static void main(String[] args){  String uri ="http://localhost:8080/...."
    Map<String,Object> map =new HashMap<>();
    map.put("key","value")  
    map.put("key","value")
   JsonObject json =new JsonObject();    
   json.put("proxyHost","127.0.0.1");
   jsonObject.put("proxyPort","8080");
   jsonObject.put("paramNumber","5");
   StringBuffer stringBuffer =httpSaveFromDate(map, uri, jsonObject);
  System.out.println(stringBuffer);

}
感谢:.....找不到那个参考的博客了但是还是感谢大佬吧...

相关推荐