1. 程式人生 > >java跨域後臺傳送get請求,獲取返回值

java跨域後臺傳送get請求,獲取返回值

 @RequestMapping(value = "/subscribeChannel", method = RequestMethod.GET)
    @ResponseBody
    public String subscribeChannel(String rtmpUrl){
        String result="";
      try{
          if(rtmpUrl ==null || "".equals(rtmpUrl)){
              rtmpUrl ="xxxxx請求url";
          }
          System.out
.println("進入視訊播放頁面:"+rtmpUrl); // 根據地址獲取請求 HttpGet request = new HttpGet(rtmpUrl);//這裡傳送get請求 request.setHeader("Connection","close"); // 獲取當前客戶端物件 HttpClient httpClient = new DefaultHttpClient(); // 通過請求物件獲取響應物件 HttpResponse response = httpClient.execute(request); // 判斷網路連線狀態碼是否正常(0--200都數正常)
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { result= EntityUtils.toString(response.getEntity(),"utf-8"); }else{ result= EntityUtils.toString(response.getEntity(),"utf-8"); } System.out.println("成功json物件:"+result); if
(result == null || "".equals(result)){ result = "{\"data\":{},\"message\":\"請求失敗,視訊伺服器未啟動\",\"statusCode\":\"100100\"}"; // throw new HttpException("播放異常!"); } }catch (Exception e){ e.printStackTrace(); result = "{\"data\":{},\"message\":\"請求失敗,視訊伺服器未啟動\",\"statusCode\":\"100100\"}"; // throw new HttpException("播放異常!"); } return result; }