skip to Main Content

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: $typen");
    // 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

2

Answers


  1. Chosen as BEST ANSWER

    Thanks for quick and good solution.

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


  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search