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", "gsbf@2018");
            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("gsbf@2018", "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();
        }
    }


猜你喜欢

转载自blog.csdn.net/myblogzz/article/details/80248822