public String doPostAdd(String urlStr, String json,String token) { StringBuilder sb = new StringBuilder(); String url = urlStr; // 创建连接对象 HttpClient httpClient = new HttpClient(); // 创建请求 PostMethod method = new PostMethod(url); // 设置请求头格式为json格式 RequestEntity entity = null; try { entity = new StringRequestEntity(json, "application/json", "UTF-8"); // 设置请求体信息 if (entity == null){ return ""; } method.setRequestEntity(entity); // 设置请求头信息 method.setRequestHeader(HttpHeaders.AUTHORIZATION,"Bearer "+token); // 创建连接 int code = httpClient.executeMethod(method); System.out.println("code:"+code); // 获取返回信息 InputStream in = method.getResponseBodyAsStream(); InputStreamReader isr = new InputStreamReader(in, "UTF-8"); char[] b = new char[4096]; for (int n; (n = isr.read(b)) != -1;) { sb.append(new String(b, 0, n)); } } catch (UnsupportedEncodingException e) { System.out.println("=====JSON数据转换有误。参数json数据:" + json + "END====="); e.printStackTrace(); } catch (HttpException e) { System.out.println("=====URL连接失败。url:" + url + "END====="); e.printStackTrace(); } catch (IOException e) { System.out.println("=====参数传递失败。END====="); e.printStackTrace(); } return sb.toString(); }
02-24
2355
10-26
2557
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交