使用http MultipartEntityBuilder发送请求
//MultipartEntityBuilder发送请求方法体
//号码导入接口
public static String importnumber(Map<String,Object> datastr,String tokenid){
//供应商提供的接口地址
String url = "http://192.168.1.1:8080/interface.php?func=importnumber";
String charset = "UTF-8";
JSONObject jsonObject = null;
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
Map<String,Object> resultMap = new HashMap<String,Object>();
//把一个普通参数和文件上传给下面这个地址 是一个servlet
HttpPost httpPost = new HttpPost(url);
//设置传输参数
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
//设计文件以外的参数
Set<String> keySet = datastr.keySet();
for (String key : keySet) {
//相当于<input type="text" name="name" value=name>
multipartEntity.addPart(key, new StringBody((String) datastr.get(key), ContentType.create("text/plain", Consts.UTF_8)));
}
HttpEntity reqEntity = multipartEntity.build();
httpPost.setEntity(reqEntity);
//Logger.info("发起请求的页面地址 " + httpPost.getRequestLine());
//发起请求 并返回请求的响应
CloseableHttpResponse response = httpClient.execute(httpPost);
try {
//log.info("----------------------------------------");
//打印响应状态
//log.info(response.getStatusLine());
resultMap.put("statusCode", response.getStatusLine().getStatusCode());
//获取响应对象
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
//打印响应长度
System.out.println("Response content length: " + resEntity.getContentLength());
//log.info("Response content length: " + resEntity.getContentLength());
//打印响应内容
resultMap.put("data", EntityUtils.toString(resEntity, Charset.forName("UTF-8")));
}
//销毁
EntityUtils.consume(resEntity);
} catch (Exception e) {
e.printStackTrace();
} finally {
response.close();
}
//RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(TIMEOUT).setConnectTimeout(TIMEOUT).build();//设置请求和传输超时时�?
//HttpPost httppost = new HttpPost(url.trim());
//httppost.setConfig(requestConfig);
//if (datastr != null) {
// if (charset != null) {
// httppost.setEntity(new StringEntity(datastr, charset));
// } else {
// httppost.setEntity(new StringEntity(datastr));
// }
//}
//CloseableHttpResponse response = httpclient.execute(httppost);
String result = null;
//if(response.getStatusLine().getStatusCode()==200){
// result = EntityUtils.toString(response.getEntity());
// jsonObject = JSONObject.parseObject(result);
// System.out.println(result);
//}
//httpclient.close();
System.out.println("导入号码完成");
}
//try {
// CloseableHttpClient httpclient = HttpClients.createDefault();
// RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(TIMEOUT).setConnectTimeout(TIMEOUT).build();//设置请求和传输超时时�?
// HttpPost httppost = new HttpPost(url.trim());
// httppost.setConfig(requestConfig);
// if (datastr != null) {
// if (charset != null) {
// httppost.setEntity(new StringEntity(datastr, charset));
// } else {
// httppost.setEntity(new StringEntity(datastr));
// }
// }
// CloseableHttpResponse response = httpclient.execute(httppost);
// String result = null;
//
// if(response.getStatusLine().getStatusCode()==200){
// result = EntityUtils.toString(response.getEntity());
// jsonObject = JSONObject.parseObject(result);
// System.out.println(result);
// }
// httpclient.close();
// System.out.println("导入号码完成");
//}
catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "导入号码完成";
}
//调用方法开始位置
public static void SendPhone(List<HashMap<String, String>> phoneList) throws UnsupportedEncodingException {
Map<String,Object> json = new HashMap<String,Object>();
JSONObject json2 = new JSONObject();
String tokenid = getTokenId();
String prjid = getPrjid(tokenid);
String subid = createsubscribe(tokenid,prjid,"未检测人员外呼");
Array array[] = {};
for (int i = 0; i < phoneList.size(); i ++) {
String phones = phoneList.get(i).get("phone");
System.out.println("i=" + i + ",phones:" + phones);
json2.put(phoneList.get(i).get("phone"),array);
}
//json2.put("19163321515",array);
json.put("tokenid",tokenid);
json.put("prjid",prjid);
json.put("subid",subid);
json.put("total", Integer.toString(phoneList.size()));
json.put("data",encode(json2).trim());
json.put("duplicates","1");
//importnumber(json.toJSONString(),tokenid);
importnumber(json,tokenid);
System.out.println("结束");
}

3520

被折叠的 条评论
为什么被折叠?



