A. java怎么调用接口
String sendPost(String jsonStr, String path)x0dx0a throws IOException {x0dx0a byte[] data = jsonStr.getBytes();x0dx0a java.net.URL url = new java.net.URL(path);x0dx0a java.net.HttpURLConnection conn = x0dx0a (java.net.HttpURLConnection) url.openConnection();x0dx0a conn.setRequestMethod("POST");x0dx0a conn.setConnectTimeout(5 * 1000);// 设置连接超时时间为5秒 x0dx0a conn.setReadTimeout(20 * 1000);// 设置读取超时时间为20秒 x0dx0a // 使用 URL 连接进行输出,则将 DoOutput标志设置为 truex0dx0a conn.setDoOutput(true);x0dx0a x0dx0a conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");x0dx0a //conn.setRequestProperty("Content-Encoding","gzip");x0dx0a conn.setRequestProperty("Content-Length", String.valueOf(data.length));x0dx0a OutputStream outStream = conn.getOutputStream();// 返回写入到此连接的输出流x0dx0a outStream.write(data);x0dx0a outStream.close();//关闭流x0dx0a String msg = "";// 保存调用http服务后的响应信息x0dx0a // 如果请求响应颂旅码是200,则表示成功x0dx0a if (conn.getResponseCode() == 200) {x0dx0a // HTTP服务端返回的编码坦樱陪是UTF-8,故必须设置为UTF-8,保持编码统一,否则会出现中文乱码x0dx0a BufferedReader in = new BufferedReader(new InputStreamReader(x0dx0a (InputStream) conn.getInputStream(), "UTF-8"));x0dx0a msg = in.readLine();x0dx0a in.close();x0dx0a }x0dx0a conn.disconnect();// 断开连接让蠢x0dx0a return msg;x0dx0a }