java – 从Google云端硬盘获取可流式链接[代码准备就绪,访问被拒绝]

前端之家收集整理的这篇文章主要介绍了java – 从Google云端硬盘获取可流式链接[代码准备就绪,访问被拒绝]前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试重复从Google云端硬盘获取可下载链接的过程,就像在Internet下载管理器( Windows)中使用的那样.

我正在做以下事情:

>下载链接的HTML源代码.
>使用正则表达式搜索fmt_stream_map.
>解码.
>尝试联系该链接.
>访问被拒绝.

在Internet下载管理器中使用相同的方法,并且它运行良好.我正在使用我的手机获取链接,并通过手机访问它.所以基本上它是相同的IP和相同的设备.

我的代码首先下载源代码.搜索地图列表,然后将质量及其描述存储在数组中.之后,我搜索链接的fmt_stream_map,并将它们添加到最终模型中,以便轻松访问它们.

我有3个类,其中两个是模型,最后一个是此过程的主类.

  1. public class ItemStreamList {
  2.  
  3. private String quality;
  4. private String description;
  5.  
  6. public ItemStreamList(){
  7.  
  8. }
  9.  
  10. public ItemStreamList(String quality,String description){
  11. this.quality = quality;
  12. this.description = description;
  13. }
  14.  
  15. public String getQuality() {
  16. return quality;
  17. }
  18.  
  19. public void setQuality(String quality) {
  20. this.quality = quality;
  21. }
  22.  
  23. public String getDescription() {
  24. return description;
  25. }
  26.  
  27. public void setDescription(String description) {
  28. this.description = description;
  29. }
  30. }
  31.  
  32.  
  33.  
  34.  
  35.  
  36. public class ItemLink {
  37.  
  38. private String quality;
  39. private String qualityDesc;
  40. private String link;
  41.  
  42. public ItemLink(){
  43.  
  44. }
  45.  
  46. public ItemLink(String quality,String qualityDesc,String link){
  47. this.quality = quality;
  48. this.qualityDesc = qualityDesc;
  49. this.link = link;
  50. }
  51.  
  52. public String getQualityDesc() {
  53. return qualityDesc;
  54. }
  55.  
  56. public void setQualityDesc(String qualityDesc) {
  57. this.qualityDesc = qualityDesc;
  58. }
  59.  
  60. public String getLink() {
  61. return link;
  62. }
  63.  
  64. public void setLink(String link) {
  65. this.link = link;
  66. }
  67.  
  68. public String getQuality() {
  69. return quality;
  70. }
  71.  
  72. public void setQuality(String quality) {
  73. this.quality = quality;
  74. }
  75. }

现在我们来到主班.这是一个“有点”记录.

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.activity_main);
  5.  
  6. getPageHTML("https://drive.google.com/file/d/0B7--EhvK76QDNmduLWFZMXh1dGs/view");
  7. }
  8.  
  9. private void getPageHTML(final String mURL){
  10. new Thread(new Runnable() {
  11. @Override
  12. public void run() {
  13. try {
  14. Document mDoc = Jsoup.connect(mURL).get();
  15. String mHTML = mDoc.toString();
  16.  
  17. boolean hasSetStreamMapList = setStreamMapList(mHTML);
  18.  
  19. String mStreamMap = getMatchRegex(mHTML,"\"fmt_stream_map\",\"","\"]");
  20. mStreamMap = org.apache.commons.text.StringEscapeUtils.unescapeJava(mStreamMap);
  21. String[] mStreamMapQualities = mStreamMap.split(",");
  22.  
  23. if(hasSetStreamMapList){
  24. List<ItemLink> mLinks = new ArrayList<>();
  25.  
  26. for (int i = 0; i < mStreamMapQualities.length; i++){
  27. String[] mLinksArray = mStreamMapQualities[i].split("\\|");
  28. String mLink = mLinksArray[1];
  29. mLink = mLink.replaceAll("%2",",");
  30. mLinks.add(new ItemLink(mLinksArray[0],getQualityDescription(mLinksArray[0]),mLink));
  31. }
  32.  
  33. for (int i = 0; i < mLinks.size(); i++){
  34. Log.i("StreamMap","Quality: " + mLinks.get(i).getQuality() + " - " + mLinks.get(i).getQualityDesc() + "\n" + "Link: " + mLinks.get(i).getLink());
  35. Log.i("StreamMap","---------------------------");
  36. }
  37.  
  38. startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(mLinks.get(0).getLink())));
  39.  
  40.  
  41. } else {
  42. Log.i("StreamMap","Stream Map is NOT set");
  43. }
  44.  
  45. } catch (IOException e) {
  46. e.printStackTrace();
  47. }
  48. }
  49. }).start();
  50. }
  51.  
  52. private List<ItemStreamList> mStreamListItems;
  53.  
  54. private String getQualityDescription(String mQuality){
  55. // Loop through the Stream Map's Array
  56. for (int i = 0; i < mStreamListItems.size(); i++){
  57. // If the Quality contains the Param,then return it
  58. if(mStreamListItems.get(i).getQuality().contains(mQuality))
  59. return mStreamListItems.get(i).getDescription();
  60. }
  61. // Didn't find the quality,return null
  62. return null;
  63. }
  64.  
  65. private boolean setStreamMapList(String mSource){
  66.  
  67. // Define the Array
  68. mStreamListItems = new ArrayList<>();
  69.  
  70. // Get the fmt_list from the HTML
  71. String mStreamMapList = getMatchRegex(mSource,"\"fmt_list\","\"]");
  72. // Check if isn't null
  73. if(mStreamMapList != null){
  74. // Split the qualities by ","
  75. String[] mStreamMapListArray = mStreamMapList.split(",");
  76. // Loop through the Array
  77. for (int i = 0; i < mStreamMapListArray.length; i++){
  78. /*
  79. Split the array by "/"
  80. First index has the quality
  81. Second index has the quality's description
  82. */
  83. String[] mModelArray = mStreamMapListArray[i].split("/");
  84. mStreamListItems.add(new ItemStreamList(mModelArray[0],mModelArray[1]));
  85. }
  86.  
  87. return true;
  88.  
  89. }
  90.  
  91. // It's null or not set,return false
  92. return false;
  93. }
  94.  
  95. private String getMatchRegex(String mSource,String mFirst,String mSecond){
  96. String regexString = Pattern.quote(mFirst) + "(.*?)" + Pattern.quote(mSecond);
  97. Pattern mPattern = Pattern.compile(regexString);
  98. Matcher mMatcher = mPattern.matcher(mSource);
  99. while(mMatcher.find())
  100. {
  101. String resultString = mMatcher.group(1);
  102.  
  103.  
  104. return resultString;
  105. }
  106. return null;
  107. }

解决方法

您将收到403代码,因为您未在fmt_stream_map链接中传递请求中的任何标头.

在浏览器中打开顶部链接时,将使用所有这些标头查询fmt_stream_map链接

所需的标题没有记录,因为您不应该以这种方式集成谷歌驱动器

猜你在找的Java相关文章