1. 程式人生 > >HttpClient請求服務端介面資料Get與Post請求

HttpClient請求服務端介面資料Get與Post請求

public static void main(String[] args) {
         //String url = "http://10.3.1.32/proxy_services/MDMConsumerProxyService";
        String url = "http://10.3.1.32/proxy_services/MDMConsumerProxyService";
         try {
            URL restServiceURL = new URL(url);
            HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL.openConnection();
            httpConnection.setDoOutput(true);
            httpConnection.setRequestMethod("POST");
            httpConnection.setRequestProperty("Content-Type", "application/json");
            httpConnection.setRequestProperty("loginCode", "gsbf");
            httpConnection.setRequestProperty("loginPassword", "
[email protected]
"); httpConnection.setRequestProperty("mdmcode", "DWQUERY_V_DISP_JK_GONGSHUIBF_SSJC"); /* String input = "&name="+ URLEncoder.encode("admin", "UTF-8"); input+="&password="+ URLEncoder.encode("0000", "UTF-8");*/ //傳遞引數 /*String input = "&loginCode="+ URLEncoder.encode("gsbf", "UTF-8"); input+="&loginPassword="+ URLEncoder.encode("
[email protected]
", "UTF-8"); input+="&mdmcode="+ URLEncoder.encode("DWQUERY_V_DISP_JK_GONGSHUIBF_SSJC", "UTF-8"); input+="&appId="+ URLEncoder.encode("platform_dm.gsbf", "UTF-8");*/ String input="{\"pageSize\":\"100\"}"; OutputStream outputStream =httpConnection .getOutputStream(); outputStream.write(input.getBytes()); outputStream.flush(); System.out.println("AAA"); if (httpConnection.getResponseCode() != 200) { throw new RuntimeException( "HTTP GET Request Failed with Error code : " + httpConnection.getResponseCode()); } InputStream is = httpConnection.getInputStream(); byte[] b = new byte[is.available()]; is.read(b); String result = new String(b,"utf-8"); System.out.println(result); /*InputStream is = httpConnection.getInputStream(); String resultData = null; // 儲存處理結果 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] data = new byte[1024]; int len = 0; try { while ((len = is.read(data)) != -1) { byteArrayOutputStream.write(data, 0, len); } } catch (IOException e) { e.printStackTrace(); } resultData = new String(byteArrayOutputStream.toByteArray(),"utf-8"); System.out.println("aa++++"+resultData);*/ } catch (Exception e) { e.printStackTrace(); } }