2012-08-23 42 views
1

我有一个在浏览器和Java SE应用程序中完美工作的文件的url,但它给了我servlet中的403 forbidden错误。以下是双方的Java SE程序和servlet的403在servlet中禁止

的Java SE代码的代码

public class UrlDownload { 
    final static int size=1024; 
    public static void fileUrl(){ 
     OutputStream outStream = null; 
     URLConnection uCon = null; 

     InputStream is = null; 
     try{ 
      URL Url; 
      byte[] buf; 
      int ByteRead,ByteWritten=0; 
      Url= new URL("http://o-o---preferred---bharti-del2---v17--- lscache7.c.youtube.com/videoplayback?upn=6BFud0UQ_-0&sparams=cp%2Cgcr%2Cid%2Cip%2Cipbits%2Citag%2Cratebypass%2Csource%2Cupn%2Cexpire&fexp=900147%2C907217%2C922401%2C919804%2C920704%2C912806%2C906831%2C911406%2C913550%2C912706&key=yt1&itag=37&ipbits=8&signature=6EBF4572274A427AFF58E023CEC8B62439E0B914.BD6827306B81393BE3998FA0F0701E6F2701A3F8&mv=m&sver=3&mt=1345685891&ratebypass=yes&source=youtube&ms=au&gcr=in&expire=1345708167&ip=116.203.237.173&cp=U0hTSldLVl9LUUNOM19PRVpCOkV6WE5pcUF1NjQ5&id=9d8c9310d90eae67&quality=hd1080&fallback_host=tc.v17.cache7.c.youtube.com&type=video/mp4"); 
      outStream = new BufferedOutputStream(new 
      FileOutputStream("video")); 

      uCon = Url.openConnection(); 
      is = uCon.getInputStream(); 
      buf = new byte[size]; 
      while ((ByteRead = is.read(buf)) != -1) 
      { 
       System.out.println("Downloading file"); 
       outStream.write(buf, 0, ByteRead); 
       ByteWritten += ByteRead; 
      } 
      System.out.println("Downloaded Successfully."); 
     }catch (Exception e) { 
      e.printStackTrace(); 
     }finally { 
      try { 
       is.close(); 
       outStream.close(); 
      }catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

servlet代码

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    response.setContentType("video/mp4"); 
    String url=request.getParameter("url"); 
    URLConnection con = null; 

    BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream()); 
    InputStream in=null; 
    byte[] buffer; 
    int ByteRead,ByteWritten=0; 
    try { 
     URL dUrl=new URL(url); 
     con=dUrl.openConnection(); 
     in=con.getInputStream(); 
     buffer = new byte[1024]; 
     while ((ByteRead = in.read(buffer)) != -1) 
     { 
      System.out.println("Downloading file"); 
      out.write(buffer, 0, ByteRead); 
      ByteWritten += ByteRead; 
     } 
    } finally {    
     out.close(); 
     in.close(); 
    } 
} 

url是给出一个parameter这同样urlservlet

+0

这是从同一个客户端运行吗?也许他们检查远程IP?你确定没有涉及cookie吗? – Thilo

+0

它从同一个客户端运行。 – kaysush

+2

您的J2SE代码是硬代码url,但在servlet中,您使用了“String url = request.getParameter(”url“);”这个getParameter()方法会调用一次URLDecodee.decode()。也许这是问题所在。在你的servlet代码中打印url以确保。 – hongtium

回答

1

YouTube下载URL只能用于一次性使用 - 它们绑定到最初生成的IP范围,并在一段时间后过期。正如你在这里所做的那样,在应用程序中硬编码会导致不可避免的失败。

+0

即使非临时URL如果不通过浏览器使用也会被阻止(效率​​不高,因为您可以欺骗代理标题变量),但它几次发生在我的Google服务中。 –

+0

但是他们在我的情况下没有被阻止。它们与浏览器以及Java应用程序一起工作良好。 – kaysush

+2

您使用的网址很可能在大约五个半小时内完全停止工作。其中一个查询参数是'expire = 1345708167';该数字是距离大约5.38小时的UNIX时间戳。更改'expire'值可能会使'signature'参数无效。 – duskwuff