第一種:需要httpclient的依賴包
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.3.5</version> </dependency>
直接上代碼:
public static String callBgrsjk(String requestParams) { String url = null; JSONObject jb=new JSONObject(); jb.put("code",0); try { CloseableHttpClient httpClient = HttpClients.createDefault(); RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(300 * 1000) .setConnectTimeout(300 * 1000) .build(); url = "http://URL:Port/地址"; HttpPost post = new HttpPost(url); post.setConfig(requestConfig); post.setHeader("Content-Type","application/json;charset=utf-8"); StringEntity postingString = new StringEntity(requestParams, "utf-8"); post.setEntity(postingString); HttpResponse response = httpClient.execute(post); String content = EntityUtils.toString(response.getEntity()); System.out.println(content); return content; } catch (SocketTimeoutException e) { LoggerUtil.error("調用Dat+" + ".aService接口超時,超時時間:" + 300 + "秒,url:" + url + ",參數:" + requestParams, e); return jb.toString(); } catch (Exception e) { LoggerUtil.error("調用DataService接口失敗,url:" + url + ",參數:" + requestParams, e); return jb.toString(); } }
第二種:使用jdk中的URL
/** * 發送Http post請求 * * @param xmlInfo * json轉化成的字符串 * @param URL * 請求url * @return 返回信息 */ public static String doHttpPost(String xmlInfo, String URL) { System.out.println("發起的數據:" + xmlInfo); byte[] xmlData = xmlInfo.getBytes(); InputStream instr = null; java.io.ByteArrayOutputStream out = null; try { URL url = new URL(URL); URLConnection urlCon = url.openConnection(); urlCon.setDoOutput(true); urlCon.setDoInput(true); urlCon.setUseCaches(false); urlCon.setRequestProperty("content-Type", "application/json"); urlCon.setRequestProperty("charset", "utf-8"); urlCon.setRequestProperty("Content-length", String.valueOf(xmlData.length)); System.out.println(String.valueOf(xmlData.length)); DataOutputStream printout = new DataOutputStream( urlCon.getOutputStream()); printout.write(xmlData); printout.flush(); printout.close(); instr = urlCon.getInputStream(); byte[] bis = IOUtils.toByteArray(instr); String ResponseString = new String(bis, "UTF-8"); if ((ResponseString == null) || ("".equals(ResponseString.trim()))) { System.out.println("返回空"); } System.out.println("返回數據為:" + ResponseString); return ResponseString; } catch (Exception e) { e.printStackTrace(); return "0"; } finally { try { if(out!=null){ out.close(); } if(instr!=null){ instr.close(); } } catch (Exception ex) { return "0"; } } }
第三種:使用apache的commons包
/** * 發送post請求 * * @param params * 參數 * @param requestUrl * 請求地址 * @param authorization * 授權書 * @return 返回結果 * @throws IOException */ public static String sendPost(String params, String requestUrl, String authorization) throws IOException { byte[] requestBytes = params.getBytes("utf-8"); // 將參數轉為二進制流 HttpClient httpClient = new HttpClient();// 客戶端實例化 PostMethod postMethod = new PostMethod(requestUrl); //設置請求頭Authorization //postMethod.setRequestHeader("Authorization", "Basic " + authorization); // 設置請求頭 Content-Type postMethod.setRequestHeader("Content-Type", "application/json"); InputStream inputStream = new ByteArrayInputStream(requestBytes, 0, requestBytes.length); RequestEntity requestEntity = new InputStreamRequestEntity(inputStream, requestBytes.length, "application/json; charset=utf-8"); // 請求體 postMethod.setRequestEntity(requestEntity); httpClient.executeMethod(postMethod);// 執行請求 InputStream soapResponseStream = postMethod.getResponseBodyAsStream();// 獲取返回的流 byte[] datas = null; try { datas = readInputStream(soapResponseStream);// 從輸入流中讀取數據 } catch (Exception e) { e.printStackTrace(); } String result = new String(datas, "UTF-8");// 將二進制流轉為String // 打印返回結果 // System.out.println(result); return result; }