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

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

我正在做以下事情:

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

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

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

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

public class ItemStreamList {

    private String quality;
    private String description;

    public ItemStreamList(){

    }

    public ItemStreamList(String quality,String description){
        this.quality = quality;
        this.description = description;
    }

    public String getQuality() {
        return quality;
    }

    public void setQuality(String quality) {
        this.quality = quality;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}





public class ItemLink {

    private String quality;
    private String qualityDesc;
    private String link;

    public ItemLink(){

    }

    public ItemLink(String quality,String qualityDesc,String link){
        this.quality = quality;
        this.qualityDesc = qualityDesc;
        this.link = link;
    }

    public String getQualityDesc() {
        return qualityDesc;
    }

    public void setQualityDesc(String qualityDesc) {
        this.qualityDesc = qualityDesc;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public String getQuality() {
        return quality;
    }

    public void setQuality(String quality) {
        this.quality = quality;
    }
}

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

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getPageHTML("https://drive.google.com/file/d/0B7--EhvK76QDNmduLWFZMXh1dGs/view");
    }

    private void getPageHTML(final String mURL){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Document mDoc = Jsoup.connect(mURL).get();
                    String mHTML = mDoc.toString();

                    boolean hasSetStreamMapList = setStreamMapList(mHTML);

                    String mStreamMap = getMatchRegex(mHTML,"\"fmt_stream_map\",\"","\"]");
                    mStreamMap = org.apache.commons.text.StringEscapeUtils.unescapeJava(mStreamMap);
                    String[] mStreamMapQualities = mStreamMap.split(",");

                    if(hasSetStreamMapList){
                        List<ItemLink> mLinks = new ArrayList<>();

                        for (int i = 0; i < mStreamMapQualities.length; i++){
                            String[] mLinksArray = mStreamMapQualities[i].split("\\|");
                            String mLink = mLinksArray[1];
                            mLink = mLink.replaceAll("%2",",");
                            mLinks.add(new ItemLink(mLinksArray[0],getQualityDescription(mLinksArray[0]),mLink));
                        }

                        for (int i = 0; i < mLinks.size(); i++){
                            Log.i("StreamMap","Quality: " + mLinks.get(i).getQuality() + " - " + mLinks.get(i).getQualityDesc() + "\n" + "Link: " + mLinks.get(i).getLink());
                            Log.i("StreamMap","---------------------------");
                        }

                        startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(mLinks.get(0).getLink())));


                    } else {
                        Log.i("StreamMap","Stream Map is NOT set");
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private List<ItemStreamList> mStreamListItems;

    private String getQualityDescription(String mQuality){
        // Loop through the Stream Map's Array
        for (int i = 0; i < mStreamListItems.size(); i++){
            // If the Quality contains the Param,then return it
            if(mStreamListItems.get(i).getQuality().contains(mQuality))
                return mStreamListItems.get(i).getDescription();
        }
        // Didn't find the quality,return null
        return null;
    }

    private boolean setStreamMapList(String mSource){

        // Define the Array
        mStreamListItems = new ArrayList<>();

        // Get the fmt_list from the HTML
        String mStreamMapList = getMatchRegex(mSource,"\"fmt_list\","\"]");
        // Check if isn't null
        if(mStreamMapList != null){
            // Split the qualities by ","
            String[] mStreamMapListArray = mStreamMapList.split(",");
            // Loop through the Array
            for (int i = 0; i < mStreamMapListArray.length; i++){
                /*
                Split the array by "/"
                First index has the quality
                Second index has the quality's description
                */
                String[] mModelArray = mStreamMapListArray[i].split("/");
                mStreamListItems.add(new ItemStreamList(mModelArray[0],mModelArray[1]));
            }

            return true;

        }

        // It's null or not set,return false
        return false;
    }

    private String getMatchRegex(String mSource,String mFirst,String mSecond){
        String regexString = Pattern.quote(mFirst) + "(.*?)" + Pattern.quote(mSecond);
        Pattern mPattern = Pattern.compile(regexString);
        Matcher mMatcher = mPattern.matcher(mSource);
        while(mMatcher.find())
        {
            String resultString = mMatcher.group(1);


            return resultString;
        }
        return null;
    }

解决方法

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

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

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

原文链接:https://www.f2er.com/java/129703.html

猜你在找的Java相关文章