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 }