0

我通过下列方式使用HTTP基本身份验证的API检索数据:保持基本授权活着HttpURLConnection的

 for (int i = 1; i < 10000; i++) { 
      try { 
       URL newurl = new URL ("SOMEURL" + i + ".json"); 
       HttpURLConnection newconnection = (HttpURLConnection) newurl.openConnection(); 
       newconnection.setRequestMethod("GET"); 
       newconnection.setDoInput(true); 
       newconnection.setRequestProperty ("Authorization", "Basic " + encoding); 
       try { 
        InputStream newcontent = (InputStream)newconnection.getInputStream(); 
        BufferedReader bfReader = new BufferedReader (new InputStreamReader (newcontent));   
        String newline; 

        if ((newline = bfReader.readLine()) != null) { 
         out.println(newline); 
        } 
       } 
       catch(Exception e2) { 
       } 
      } 
      catch(Exception e) { 
       e.printStackTrace(); 
      } 
     } 

问题:与其问认证10000次,可我得到对第一个请求进行身份验证一次,保持活动状态,并在不请求身份验证的情况下完成剩余的9999个请求?

目的:处理更快,服务器负载更少。

有何评论?

回答

0

HTTP被设计为无状态,所以你的方法没有任何问题。但是,您可以(但不推荐使用API​​)会话(使用Cookie)或提供一个登录URL,该URL将发出您可以通过的临时身份验证令牌。