httpClient请求将数据传递到接口中
2021-02-04 05:16
标签:第五天 ber parameter 形式 day string tproxy 请求头 utf-8 -- from-date 形式的数据也能传 --- 获取当前时间将当前时间往后延长自定义天数 ps:多嘴说下 httpClient请求将数据传递到接口中 标签:第五天 ber parameter 形式 day string tproxy 请求头 utf-8 原文地址:https://www.cnblogs.com/Lingzsj/p/13149697.htmlpublic static StringBuffer httpSaveFromDate(Map
HttpClient httpClient = new HttpClient();
//设置代理模式
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) --返回得到的是第五天的时间httpSaveFromDate(Map
public static void main(String[] args){
String uri ="http://localhost:8080/...."
Map感谢:.....找不到那个参考的博客了但是还是感谢大佬吧...
文章标题:httpClient请求将数据传递到接口中
文章链接:http://soscw.com/index.php/essay/50753.html