spring自帶HTTP請求工具類的簡單使用

jopen 9年前發布 | 2K 次閱讀 Java

spring包路徑: org.springframework.http.client

try {
            URI uri = new URI("http://192.168.129.11:7020/public/goIndex");
            SimpleClientHttpRequestFactory schr = new SimpleClientHttpRequestFactory();
            ClientHttpRequest chr = schr.createRequest(uri, HttpMethod.POST);
            ClientHttpResponse res = chr.execute();
            InputStream is = res.getBody(); //獲得返回數據,注意這里是個流
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String str = "";
            while((str = br.readLine())!=null){
                System.out.println(str);//獲得頁面內容或返回內容
            }

    } catch (URISyntaxException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }</pre> 


補充一下,  maven工程只用到了spring-web

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</artifactId>

<version>4.2.0.RELEASE</version>

</dependency>


 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!