title: Download with Volley gives error: Unexpected response code 200
body: <p>I am trying to download a file from my server. If I make this request through the browser I get the desired file, but when I use volley I first got com.android.volley.TimeoutError after I use setRetryPolicy I get </p>
  
  <pre><code>BasicNetwork.performRequest: Unexpected response code 200
  </code></pre>
  
  <p>Here is my class that makes the download:</p>
  
  <pre><code>public class DefexConnection implements Response.Listener&lt;byte[]&gt;, Response.ErrorListener {
  private Context ctx;
  private DefexDownload download;
  public DefexConnection(Context ctx){
      this.ctx = ctx;
  }
  
  
  public void download(String link, final HashMap&lt;String, String&gt; params){
      DefexDownload req = new DefexDownload(Request.Method.GET, link, DefexConnection.this, DefexConnection.this, params);
      req.setRetryPolicy(new DefaultRetryPolicy( 500000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
      RequestQueue queue = Volley.newRequestQueue(this.ctx, new HurlStack(null, getSocketFactory()));
      queue.add(req);
  }
  
  
  
  
  private SSLSocketFactory getSocketFactory() {
  
      CertificateFactory cf = null;
      try {
          cf = CertificateFactory.getInstance("X.509");
          InputStream caInput = ctx.getResources().openRawResource(R.raw.comodoroca);
          Certificate ca;
          try {
              ca = cf.generateCertificate(caInput);
              Log.e("CERT", "ca=" + ((X509Certificate) ca).getSubjectDN());
          } finally {
              caInput.close();
          }
  
  
          String keyStoreType = KeyStore.getDefaultType();
          KeyStore keyStore = KeyStore.getInstance(keyStoreType);
          keyStore.load(null, null);
          keyStore.setCertificateEntry("ca", ca);
  
  
          String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
          TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
          tmf.init(keyStore);
  
  
          HostnameVerifier hostnameVerifier = new HostnameVerifier() {
              @Override
              public boolean verify(String hostname, SSLSession session) {
  
                  Log.d("CipherUsed", session.getCipherSuite());
                  return hostname.compareTo("www.defexsecurity.com")==0; 
  
              }
          };
  
  
          HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
          SSLContext context = null;
          context = SSLContext.getInstance("TLS");
  
          context.init(null, tmf.getTrustManagers(), null);
          HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
  
          SSLSocketFactory sf = context.getSocketFactory();
  
  
          return sf;
  
      } catch (CertificateException e) {
          e.printStackTrace();
      } catch (NoSuchAlgorithmException e) {
          e.printStackTrace();
      } catch (KeyStoreException e) {
          e.printStackTrace();
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      } catch (KeyManagementException e) {
          e.printStackTrace();
      }
  
      return  null;
  }
  
  
  @Override
  public void onResponse(byte[] response) {
      HashMap&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();
      int count;
      if (response!=null) {
          String content = download.responseHeaders.get("Content-Disposition").toString();
          StringTokenizer st = new StringTokenizer(content, "=");
          String[] arrTag = st.toArray();
  
          String filename = arrTag[1];
          filename = filename.replace(":", ".");
          Log.d("DEBUG::RESUME FILE NAME", filename);
          try{
              long lenghtOfFile = response.length;
  
              //covert reponse to input stream
              InputStream input = new ByteArrayInputStream(response);
              File path = Environment.getExternalStorageDirectory();
              File file = new File(path, filename);
              map.put("resume_path", file.toString());
              BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(file));
              byte data[] = new byte[1024];
  
              long total = 0;
  
              while ((count = input.read(data)) != -1) {
                  total += count;
                  output.write(data, 0, count);
              }
  
              output.flush();
  
              output.close();
              input.close();
              Log.e("DEFEX","file saved to "+file.getAbsolutePath());
          }catch(IOException e){
              e.printStackTrace();
  
          }
      }
  }
  
  @Override
  public void onErrorResponse(VolleyError error) {
      Log.e("DEFEX", "UNABLE TO DOWNLOAD FILE. ERROR:: "+error.toString());
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Here is my class DefexDownload (which extends Request):</p>
  
  <pre><code>class DefexDownload extends Request&lt;byte[]&gt;{
  
      private final Response.Listener&lt;byte[]&gt; mListener;
      private Map&lt;String, String&gt; mParams;
      public Map&lt;String, String&gt; responseHeaders ;
  
  
      DefexDownload(int post, String mUrl, Response.Listener&lt;byte[]&gt; listener, Response.ErrorListener errorListener, HashMap&lt;String, String&gt; params){
          super(post, mUrl, errorListener);
          setShouldCache(false);
          mListener = listener;
          mParams=params;
      }
  
      @Override
      protected Map&lt;String, String&gt; getParams() throws com.android.volley.AuthFailureError {
          return mParams;
      }
  
      @Override
      protected void deliverResponse(byte[] response) {
          mListener.onResponse(response);
      }
  
      @Override
      protected Response&lt;byte[]&gt; parseNetworkResponse(NetworkResponse response) {
          //Initialise local responseHeaders map with response headers received
          responseHeaders = response.headers;
  
          //Pass the response data here
          return Response.success(response.data, HttpHeaderParser.parseCacheHeaders(response));
      }
  
  }
  </code></pre>
  
  <p>I have no clue what could be wrong here, any help will be gratly appreciated</p>
  
  <p>Edit: on every other answer I found here on SO the problems seems to be the proxy or device emulator, but I'm running my app on my real device which is not using any proxy</p>