private RawResponse getResponse ( URL url , HttpURLConnection conn , CookieJar cookieJar , String method ) throws IOException { // read result int status = conn . getResponseCode ( ) ; String host = url . getHost ( ) . toLowerCase ( ) ; String statusLine = null ; // headers and cookies List < Header > headerList = new ArrayList <> ( ) ; List < Cookie > cookies = new ArrayList <> ( ) ; int index = 0 ; while ( true ) { String key = conn . getHeaderFieldKey ( index ) ; String value = conn . getHeaderField ( index ) ; if ( value == null ) { break ; } index ++ ; //status line if ( key == null ) { statusLine = value ; continue ; } headerList . add ( new Header ( key , value ) ) ; if ( key . equalsIgnoreCase ( NAME_SET_COOKIE ) ) { Cookie c = Cookies . parseCookie ( value , host , Cookies . calculatePath ( url . getPath ( ) ) ) ; if ( c != null ) { cookies . add ( c ) ; } } } Headers headers = new Headers ( headerList ) ; InputStream input ; try { input = conn . getInputStream ( ) ; } catch ( IOException e ) { input = conn . getErrorStream ( ) ; } if ( input == null ) { input = InputStreams . empty ( ) ; } // update session cookieJar . storeCookies ( cookies ) ; return new RawResponse ( method , url . toExternalForm ( ) , status , statusLine == null ? "" : statusLine , cookies , headers , input , conn ) ; }
Wrap response deal with headers and cookies