delight-im / Android-AdvancedWebView

Enhanced WebView component for Android that works as intended out of the box
MIT License
2.39k stars 574 forks source link

Unable to download correct .zip file on this website: https://resident.uidai.gov.in/offline-kyc #254

Open SagarGiri2104 opened 4 years ago

SagarGiri2104 commented 4 years ago

I am using advance Webview to download .zip file in my app from UIDAI website. But when DownloadListener is getting called It returns the correct data, but downloads it's HTML webpage in that .zip file instead of downloading the actual file. whereas when I try downloading the same zip via chrome. It is downloading the correct zip file. Please help me with this problem. why is this happening in webview on this particular website (https://resident.uidai.gov.in/offline-kyc) whereas with the use of same onDownloadRequested function I am able to download files on other websites?

My code:


public class MainActivity 
   extends AppCompatActivity implements AdvancedWebView.Listener {
     private static final String QT_TAG = "QT_TAG";
     private AdvancedWebView mWebView;

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

        mWebView = (AdvancedWebView) findViewById(R.id.webview);
        mWebView.setListener(this, this);
        String url = "https://resident.uidai.gov.in/offline-kyc";
        mWebView.loadUrl(url);
    }

    @Override
    public void onDownloadRequested(String url, String suggestedFilename, String mimeType,
                                    long contentLength, String contentDisposition, String userAgent) {
        Log.i(QT_TAG, "downloadlistener0: "
                + userAgent +" " + mimeType+" " +  contentLength + " "
                + contentDisposition+ " " + url +"  "+suggestedFilename);

        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

        request.setMimeType(mimeType);
        request.setDestinationUri(Uri.fromFile(new File( Environment.DIRECTORY_DOWNLOADS)));
        //------------------------COOKIE!!------------------------
        String cookies = CookieManager.getInstance().getCookie(url);
        request.addRequestHeader("cookie", cookies);
        //------------------------COOKIE!!------------------------
        request.addRequestHeader("User-Agent", userAgent);
        request.setDescription("Downloading file...");
        request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType));
        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimeType));
        DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        dm.enqueue(request);
        Toast.makeText(getApplicationContext(), "Downloading File0 ", Toast.LENGTH_LONG).show();
    }

    @SuppressLint("NewApi")
    @Override
    protected void onResume() {
        super.onResume();
        mWebView.onResume();
        Log.i(QT_TAG, "onResume: ");
        // ...
    }

    @SuppressLint("NewApi")
    @Override
    protected void onPause() {
        mWebView.onPause();
        Log.i(QT_TAG, "onPause: ");
        // ...
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        mWebView.onDestroy();
        Log.i(QT_TAG, "onDestroy: ");
        // ...
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (intent.getData() != null){
            Log.i(QT_TAG, "onActivityResult: "+ requestCode+" "+ resultCode+" "+intent.getData().toString());
        }else{
            Log.i(QT_TAG, "onActivityResult: "+ requestCode+" "+ resultCode);
        }

        mWebView.onActivityResult(requestCode, resultCode, intent);
        // ...
    }

    @Override
    public void onBackPressed() {
        if (!mWebView.onBackPressed()) {
            Log.i(QT_TAG, "onBackPressed: ");
            return; }
        // ...
        super.onBackPressed();
    }

    @Override
    public void onPageStarted(String url, Bitmap favicon) {
        Log.i(QT_TAG, "onPageStarted: "+ url);
    }

    @Override
    public void onPageFinished(String url) {
        Log.i(QT_TAG, "onPageFinished: "+ url);
    }

    @Override
    public void onPageError(int errorCode, String description, String failingUrl) {
        Log.i(QT_TAG, "onPageError "+ errorCode + " "+ description  +" "+failingUrl);
    }

    @Override
    public void onExternalPageRequest(String url) {
        Log.i(QT_TAG, "onExternalPageRequest: "+ url);

    }
}
`
ashish986 commented 4 years ago

Hi buddy,

i am facing same problem as it is. i didn't found any solutions on google. I am able to download some tricky codes but and got xml data . but i want to get same file from webview.

gouravbansal commented 4 years ago

@ ashish986 please help me out with tricky code which you used to get xml data.

rajeevmnnit commented 4 years ago

@ ashish986 Help me out with tricky code which you used to download XML data from https://resident.uidai.gov.in/offline-kyc

ashish986 commented 4 years ago

hi buddy

yes its a tricky code. but i got solution with help of chrometab. you should use chrometab in place of webview. i tried all webview tricks but unable to download xml uidiai. but when i use chrometab i got actual file and extract code. i hope this information helpful for you.

Regards Ashish

On Thu, Feb 6, 2020 at 3:41 PM rajeevmnnit notifications@github.com wrote:

@ ashish986 Help me out with tricky code which you used to download XML data from https://resident.uidai.gov.in/offline-kyc

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/delight-im/Android-AdvancedWebView/issues/254?email_source=notifications&email_token=AC5EZNBQLNBZDZVP24INNCLRBPO6FA5CNFSM4I3DJZT2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEK6USQA#issuecomment-582830400, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC5EZNEKLUOYJUOUUWKL5ZTRBPO6FANCNFSM4I3DJZTQ .

ashish986 commented 4 years ago

hi buddy

yes its a tricky code. but i got solution with help of chrometab. you should use chrometab in place of webview. i tried all webview tricks but unable to download xml uidiai. but when i use chrometab i got actual file and extract code. i hope this information helpful for you.

On Tue, Jan 21, 2020 at 8:42 PM gouravbansal notifications@github.com wrote:

@ ashish986 please help me out with tricky code which you used to get xml data.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/delight-im/Android-AdvancedWebView/issues/254?email_source=notifications&email_token=AC5EZNF5NZ4NZU7L5BJRYX3Q64GEVA5CNFSM4I3DJZT2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEJQCK7Y#issuecomment-576726399, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC5EZNA26HDMRPI5HHSCTHTQ64GEVANCNFSM4I3DJZTQ .

gouravbansal commented 4 years ago

@ashish986 is this working on devices without chrome..

ashish986 commented 4 years ago

hi this is working without chrome. but will work on normal web browser.

On Tue, Feb 25, 2020 at 9:58 AM gouravbansal notifications@github.com wrote:

@ashish986 https://github.com/ashish986 is this working on devices without chrome..

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/delight-im/Android-AdvancedWebView/issues/254?email_source=notifications&email_token=AC5EZNDZFEOGGUIWO6MF5ADRESM5VA5CNFSM4I3DJZT2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEM2QK6A#issuecomment-590677368, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC5EZNGI73OYX2KFSWWIOWLRESM5VANCNFSM4I3DJZTQ .

gouravbansal commented 4 years ago

hi 1, what you means by normal web browser.. 2, how you get information of file completely downloaded..

ashish986 commented 4 years ago

hi

chrometab working with chrome browser. if you want download from default browser u should call programmatically browser.

On Tue, Feb 25, 2020 at 10:48 AM gouravbansal notifications@github.com wrote:

hi 1, what you means by normal web browser.. 2, how you get information of file completely downloaded..

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/delight-im/Android-AdvancedWebView/issues/254?email_source=notifications&email_token=AC5EZNDZKEJNHMWN3TYT76TRESS35A5CNFSM4I3DJZT2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEM2TDZQ#issuecomment-590688742, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC5EZNATPM7N4UXTUAGEEV3RESS35ANCNFSM4I3DJZTQ .

gouravbansal commented 4 years ago

@ashish986 Hi Not able to download using chrome tab.. can you share sample code

ashish986 commented 4 years ago

ok

Uri uri = Uri.parse("https://resident.uidai.gov.in/offline-kyc"); // Intent intent = new Intent(getApplicationContext(), CustomTabReceiver.class); CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder(); CustomTabsIntent customTabsIntent = intentBuilder.build(); intentBuilder.setToolbarColor(ContextCompat.getColor(this, R.color.colorPrimary)); intentBuilder.setSecondaryToolbarColor(ContextCompat.getColor(this, R.color.colorPrimaryDark));

// set start and exit animations intentBuilder.setStartAnimations(this, android.R.anim.slide_in_left, R.anim.slide_right_out); intentBuilder.setExitAnimations(this, android.R.anim.slide_in_left, android.R.anim.slide_out_right); customTabsIntent.intent.setPackage(CHROME_PACKAGE_NAME); customTabsIntent.launchUrl(MainActivity.this, uri);

On Tue, Feb 25, 2020 at 11:04 AM gouravbansal notifications@github.com wrote:

@ashish986 https://github.com/ashish986 Hi Not able to download using chrome tab.. can you share sample code

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/delight-im/Android-AdvancedWebView/issues/254?email_source=notifications&email_token=AC5EZNDJ742GJ4IAKNCP6SLRESUXHA5CNFSM4I3DJZT2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEM2UAXA#issuecomment-590692444, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC5EZNBQUNGO4QBZKCVQDKDRESUXHANCNFSM4I3DJZTQ .

nurul12121 commented 4 years ago

@ashish986 Hi Ashish. I am trying to implement the same in android and facing the same issue. 1) Is there no other way to do the same in WebView? 2) If using CustomTabsIntent, how to get callback once zip file is downloaded in CustomTabsIntent? Thanks.

suvansh19 commented 4 years ago

@nurul12121 Any workaround you found for this?

ashutoshnagar commented 4 years ago

@nurul12121 @suvansh19 Did you guy find any work around this?

ashish986 commented 4 years ago

What do u want code or else?

On Sun 19 Jul, 2020, 8:32 PM Ashutosh Nagar, notifications@github.com wrote:

@nurul12121 https://github.com/nurul12121 @suvansh19 https://github.com/suvansh19 Did you guy find any work around this?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/delight-im/Android-AdvancedWebView/issues/254#issuecomment-660658925, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC5EZNG7FIG24PO2PZN7PGTR4MDHZANCNFSM4I3DJZTQ .

ashutoshnagar commented 4 years ago

@ashish986 Actually, corrupt file is getting downloaded through webview, so wanted to know if anyone found solution, if not then how to use custom tabs, never used it..can you pls help

And if you can share code that will be really helpful

ashish986 commented 4 years ago

its not corrupt file . its download html request file. dont use webview if u want to download use browser open link on browser and get download. or use custom tabs.

On Sun, Jul 19, 2020 at 10:30 PM Ashutosh Nagar notifications@github.com wrote:

@ashish986 https://github.com/ashish986 Actually, corrupt file is getting downloaded through webview, so wanted to know if anyone found solution, if not then how to use custom tabs, never used it..can you pls help

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/delight-im/Android-AdvancedWebView/issues/254#issuecomment-660676359, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC5EZNGDJXWU2KBH3JQOKUDR4MREXANCNFSM4I3DJZTQ .

ocram commented 4 years ago

Is that download a HTTP POST download perhaps?

Android’s DownloadManager is designed to work with downloads that (a) don’t require any cookies and (b) are performed via HTTP GET. Cookie support can be added easily, as can be seen in one of the comments above, but that still leaves us with the restriction to the GET request method.

If your download requires HTTP POST, you should still be able to use the onDownloadRequested callback, but not the DownloadManager.Request instance inside. Just check if the URL passed to that callback is actually valid. If it is, replace the DownloadManager.Request instance with your own code for a HTTP request that downloads and stores the file from the specified URL.

Does that help?

suvDev commented 4 years ago

@ashutoshnagar No.

ashutoshnagar commented 4 years ago

@ocram Yes, its a POST request. Can you please help me understand in detail or share some link which I can refer. New to coding world.

ashutoshnagar commented 4 years ago

@ashish986 Can we open browser link on DownloadRequested and download the file and come back on app without user noticing it or can we control the custom tab functionality from app, like we can do for webview?

ashish986 commented 4 years ago

Use custom tab u can broswe the link as download request and download file the file but u can't back on app cause u cant manage from app request. But u can download the file and extract the zip progrmatically.

On Tue 21 Jul, 2020, 9:16 PM Ashutosh Nagar, notifications@github.com wrote:

@ashish986 https://github.com/ashish986 Can we open browser link on DownloadRequested and download the file and come back on app without user noticing it or can we control the custom tab functionality from app, like we can do for webview?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/delight-im/Android-AdvancedWebView/issues/254#issuecomment-661940830, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC5EZNEDEO2DMVLKALVM64TR4WZ5ZANCNFSM4I3DJZTQ .

ashutoshnagar commented 4 years ago

Thanks @ashish986 for quick reply, can we control the flow through app, just like we do in webview using js because somewhere i had read there is no js bridge between custom tab and app?

ashish986 commented 4 years ago

At this point I stucked. U heard right no connection between tab and app and also no connection Between broswer and app and for your kind information u can't download zip through webview . U can download only download request page not actual zip file.i am working on this project soon will get success.

On Tue 21 Jul, 2020, 10:05 PM Ashutosh Nagar, notifications@github.com wrote:

Thanks @ashish986 https://github.com/ashish986 for quick reply, can we control the flow through app, just like we do in webview using js because somewhere i had read there is no js bridge between custom tab and app?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/delight-im/Android-AdvancedWebView/issues/254#issuecomment-661968669, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC5EZNEBVZJELYWDMHPLNQDR4W7TJANCNFSM4I3DJZTQ .

ocram commented 4 years ago

I would listen to the onDownloadRequested callback as usual, and instead of using a DownloadManager instance there, with the url given, use HttpClient and HttpPost as shown here while removing the three (!) lines for HTTP GET from that example so that you actually store the response (HttpResponse) of the HTTP POST request.

Does that help?

If your HTTP POST request requires parameters, though, just using the url from onDownloadRequested will not be enough. You should go to your desktop computer, open your web browser, open the developer tools and switch to the network tab to check if the download requires parameters.

ashutoshnagar commented 4 years ago

@ocram It didnt work, it is still downloading a corrupt zip file

ocram commented 4 years ago

Does it need cookies, or a certain referrer header, or the same user-agent string as with the previous requests perhaps?

tusharuit25 commented 3 years ago

does anyone got solution for this in flutter ?

Atindrasharma commented 2 years ago

Any solution now in web view I also facing this