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);
}感谢:.....找不到那个参考的博客了但是还是感谢大佬吧...
相关推荐
84487600 2020-08-16
似水流年梦 2020-08-09
knightwatch 2020-07-26
标题无所谓 2020-06-14
sicceer 2020-06-12
yanghui0 2020-06-09
yanghui0 2020-06-09
创建一个 HttpClient 实例,这个实例需要调用 Dispose 方法释放资源,这里使用了 using 语句。接着调用 GetAsync,给它传递要调用的方法的地址,向服务器发送 Get 请求。
wanghongsha 2020-06-04
jiaguoquan00 2020-05-26
zhaolisha 2020-05-16
wanghongsha 2020-05-05
wanghongsha 2020-04-14
knightwatch 2020-04-11
hygbuaa 2020-03-27
zergxixi 2020-03-24
stoneechogx 2020-02-13