Home > Enterprise >  OkHttp3 response string show html file instaed of download file data
OkHttp3 response string show html file instaed of download file data

Time:10-15

I using Okhttp3 for download file from server in android application. my link is http://www.webweb.infinityfreeapp.com/lichi/download.php?path=Add.jpg it download file in firefox, chorme smoothly, while in okhttp3 response string shows

<html><body><script>document.cookie="_test=9e105a99e90025d241c180c29fad3231 ; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/" ;document.location.href="http://www.webweb.infinityfreeapp.com/lichi/download.php?path=Add.jpg&i=1";</script></body></html>

but i feel response string has Add.jpg file data. so, what can i change in okhttp3 code or php code that i gather App.jpg data in response string of okhttp3

Php Code

if(isset($_GET['path']))
{
    $url = $_GET['path'];
    $type   = "application/pdf";
    $completePath = "http://www.webweb.infinityfreeapp.com/lichi/";
    $visibleName = "$url";
    $completePath .= $url; 
    // Force download
    header("Content-disposition: attachment; filename=$visibleName"); 
    header("Content-Type: application/force-download"); 
    header("Content-Transfer-Encoding: $type\n");
    // header("Content-Length: ".filesize($completePath)); 
    header("Pragma: no-cache"); 
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0, public"); 
    header("Expires: 0"); 
    readfile($completePath); 
    die();
}

I comment Content-Length because it crash system in uncomment

Java code

   OkHttpClient client = new OkHttpClient();
    String url = "http://www.webweb.infinityfreeapp.com/lichi/download.php?path=Add.jpg";
    Call call = client.newCall(new Request.Builder().url(url).get().build());
   
      Response response = call.execute();
      if (response.code() == 200 || response.code() == 201) {
           Headers responseHeaders = response.headers();
           for (int i = 0; i < responseHeaders.size(); i  ) 
              Log.d(LOG_TAG, responseHeaders.name(i)   ": "   responseHeaders.value(i));
                    
            String str = response.body().string();
     }

Here str contain above html file information instead Add.jpg file data. so please give answer

CodePudding user response:

good question

Autually if we send a get request to

http://www.webweb.infinityfreeapp.com/lichi/download.php?path=Add.jpg

we get the right resutl just like

<html><body><script>document.cookie="_test=9e105a99e90025d241c180c29fad3231 ; expires=Thu, 31-D...";</script></body></html> .

we can get a file in browser, because browser can parse html , when browser get the string result which is a html page, it create another request with a new Header (Cookie=_test=9e105a99e90025d241c180c29fad3231), and with the Cookie, we get an image file from server.

CodePudding user response:

Thanks for quick and good solution.

i just add header as:- .header("Cookie", "_test=9e105a99e90025d241c180c29fad3231") and send again with above code, actual result come

  • Related