Return-Path: Delivered-To: apmail-hc-httpclient-users-archive@www.apache.org Received: (qmail 57691 invoked from network); 13 Oct 2010 17:31:43 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 13 Oct 2010 17:31:43 -0000 Received: (qmail 87624 invoked by uid 500); 13 Oct 2010 17:31:43 -0000 Delivered-To: apmail-hc-httpclient-users-archive@hc.apache.org Received: (qmail 87561 invoked by uid 500); 13 Oct 2010 17:31:42 -0000 Mailing-List: contact httpclient-users-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpClient User Discussion" Delivered-To: mailing list httpclient-users@hc.apache.org Received: (qmail 87553 invoked by uid 99); 13 Oct 2010 17:31:42 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 13 Oct 2010 17:31:42 +0000 X-ASF-Spam-Status: No, hits=-5.0 required=10.0 tests=NORMAL_HTTP_TO_IP,RCVD_IN_DNSWL_HI,SPF_HELO_PASS,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of fmudnal@visa.com designates 198.241.159.4 as permitted sender) Received: from [198.241.159.4] (HELO visa.com) (198.241.159.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 13 Oct 2010 17:31:35 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=simple; d=visa.com; s=portal4; t=1286991073; x=1602310817; q=dns/txt; h=From:Date: Subject:Message-ID:Content-Language:Content-Type: Content-Transfer-Encoding; bh=te/ZHT/J0pWcDpcUGYZV5jTdo00ZKTuI1C aENE262Xg=; b=j1Ep3iwbKYtaPjq/bwg5GSKs0AGkQAgXMHrA9Bzc2iU1weKwb1 K44aVGElSHNcGeCp989nt2EQdj73Yxg4YCqYT1leZeH2ejMtSssjxCAXriCAfIa4 uubcUQEI1buoJag+KJIS4itWKVFkp68AztKv5Zr+bx+NqmbwX3BGffMdc= From: "Mudnal, Fayaz K" To: HttpClient User Discussion Date: Wed, 13 Oct 2010 10:30:55 -0700 Subject: RE: HTTPS redirects and then basic auth fails Thread-Topic: HTTPS redirects and then basic auth fails Thread-Index: ActqYpjHNtQ7uDs9QduY/n+hgz4xzQAASQmgACYaBQA= Message-ID: <25DD91C14FC3714F92C7BE85B81B8931030153C196@SW720MBPX016.visa.com> References: <25DD91C14FC3714F92C7BE85B81B8931030153BE09@SW720MBPX016.visa.com> In-Reply-To: <25DD91C14FC3714F92C7BE85B81B8931030153BE09@SW720MBPX016.visa.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: acceptlanguage: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Virus-Checked: Checked by ClamAV on apache.org I set the auth realm to AuthScope.ANY, but this did not work either: httpstate.setCredentials(AuthScope.ANY, credentials); Fayaz -----Original Message----- From: Mudnal, Fayaz K [mailto:fmudnal@visa.com]=20 Sent: Tuesday, October 12, 2010 4:19 PM To: httpclient-users@hc.apache.org Subject: HTTPS redirects and then basic auth fails Hi I am trying to upload a file to a https site. The site redirects 3 times an= d then login fails with a 401 error. I am using HttpClient 3.0. The server = folks said they could not see the credentials on their side. I would greatl= y appreciate any help. Here is the code: String header =3D "multipart/form-data"; PostMethod method =3D null; try { HttpClient client =3D new HttpClient(); HostConfiguration hostConfig =3D client.getHostConfiguration(); hostConfig.setHost(new URI(URL, true)); LOGGER.debug("Open connection to: " + URL); if (username !=3D null && password !=3D null && username.trim()= .length() > 0 && password.trim().length() > 0) { LOGGER.debug("Setting credentials."); Credentials credentials =3D new UsernamePasswordCredentials= (username, password); AuthScope authScope =3D new AuthScope(hostConfig.getHost(),= hostConfig.getPort()); HttpState state =3D client.getState(); state.setCredentials(authScope, credentials); LOGGER.debug("Credentials set"); List authPrefs =3D new ArrayList(3); authPrefs.add(AuthPolicy.BASIC); authPrefs.add(AuthPolicy.DIGEST); authPrefs.add(AuthPolicy.NTLM); client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIO= RITY, authPrefs); client.getParams().setAuthenticationPreemptive(true); LOGGER.debug("Preemptive Authentication set"); } RequestEntity entity =3D new InputStreamRequestEntity(inputStre= am, "application/upload"); method =3D new PostMethod(URL); method.setRequestEntity(entity); LOGGER.debug("FileInputStream set"); method.setRequestHeader("filename", fileName); method.setRequestHeader("Content-Type", header); method.setRequestHeader("Content-Disposition", "form-data"); method.setDoAuthentication(true); method.setFollowRedirects( false ); LOGGER.debug("Uploading file..."); int responseStatusCode =3D client.executeMethod(hostConfig, met= hod); LOGGER.debug("HTTPS ResponseStatusCode=3D" + responseStatusCode= ); LOGGER.debug(method.getStatusLine()); LOGGER.debug(method.getResponseBodyAsString()); // Handle redirects int redirResponseStatusCode =3D 0; if (responseStatusCode =3D=3D HttpStatus.SC_MOVED_TEMPORARILY |= | responseStatusCode =3D=3D HttpStatus.SC_MOVED_PERMANENT= LY || responseStatusCode =3D=3D HttpStatus.SC_SEE_OTHER || responseStatusCode =3D=3D HttpStatus.SC_TEMPORARY_REDIR= ECT) { LOGGER.debug("Redirection encountered:" + responseStatusCod= e); // handle a max of 10 redirects for (int i =3D 1; i < 11; i++) { LOGGER.debug("Redirect attempt: " + i); redirResponseStatusCode =3D 0; Header locationHeader =3D method.getResponseHeader("loc= ation"); if (locationHeader =3D=3D null) { throw new DeliveryException("Redirected without a loc= ation"); } String location =3D locationHeader.getValue(); hostConfig.setHost(new URI(location, true)); method.setURI(new URI(location, true)); if (username !=3D null && password !=3D null && usernam= e.trim().length() > 0 && password.trim().length() > 0) { LOGGER.debug("Setting credentials for redirect."); Credentials credentials =3D new UsernamePasswordCre= dentials(username, password); AuthScope authScope =3D new AuthScope(hostConfig.ge= tHost(), hostConfig.getPort()); HttpState state =3D client.getState(); state.setCredentials(authScope, credentials); LOGGER.debug("Credentials set"); List authPrefs =3D new ArrayList(3); authPrefs.add(AuthPolicy.BASIC); authPrefs.add(AuthPolicy.DIGEST); authPrefs.add(AuthPolicy.NTLM); client.getParams().setParameter(AuthPolicy.AUTH_SCH= EME_PRIORITY, authPrefs); //client.getState().setAuthenticationPreemptive(tru= e); client.getParams().setAuthenticationPreemptive(true= ); LOGGER.debug("Preemptive Authentication set"); } LOGGER.debug("Redirecting to location:" + location); redirResponseStatusCode =3D client.executeMethod(hostCo= nfig, method); LOGGER.debug("HTTPS RedirectResponseStatusCode=3D" + re= dirResponseStatusCode); LOGGER.debug(method.getStatusLine()); LOGGER.debug(method.getResponseBodyAsString()); if (redirResponseStatusCode !=3D HttpStatus.SC_MOVED_TE= MPORARILY && redirResponseStatusCode !=3D HttpStatus.SC_MOVE= D_PERMANENTLY && redirResponseStatusCode !=3D HttpStatus.SC_SEE_= OTHER && redirResponseStatusCode !=3D HttpStatus.SC_TEMP= ORARY_REDIRECT) break; if (i =3D=3D 10) {LOGGER.debug("MAX Redirects exceeded.= ");} } } // Handle redirects if ((responseStatusCode >=3D 400)||(redirResponseStatusCode >= =3D 400)) { LOGGER.debug("File upload via HTTPS failed."); throw new DeliveryException("File upload via HTTPS failed."= ); } else LOGGER.debug("File upload via HTTPS Successful."); } catch (Exception e) { LOGGER.debug("File upload via HTTPS failed."); e.printStackTrace(); throw new DeliveryException(e); } finally { method.releaseConnection(); } Here are the logs: [10/12/10 0:52:56:136 GMT] 00000164 SystemOut O - username:xxxxx passwo= rd:xxxxx [10/12/10 0:52:56:137 GMT] 00000164 SystemOut O - Open connection to: h= ttps://159.37.35.247/ [10/12/10 0:52:56:137 GMT] 00000164 SystemOut O - Setting credentials. [10/12/10 0:52:56:138 GMT] 00000164 SystemOut O - Credentials set [10/12/10 0:52:56:139 GMT] 00000164 SystemOut O - Preemptive Authentica= tion set [10/12/10 0:52:56:140 GMT] 00000164 SystemOut O - FileInputStream set [10/12/10 0:52:56:141 GMT] 00000164 SystemOut O - Uploading file... [10/12/10 0:52:56:452 GMT] 00000164 HttpMethodDir I org.apache.commons.http= client.HttpMethodDirector isRedirectNeeded Redirect requested but followRed= irects is disabled [10/12/10 0:52:56:459 GMT] 00000164 SystemOut O - HTTPS ResponseStatusC= ode=3D302 [10/12/10 0:52:56:459 GMT] 00000164 SystemOut O - HTTP/1.1 302 Found [10/12/10 0:52:56:459 GMT] 00000164 HttpMethodBas W org.apache.commons.http= client.HttpMethodBase getResponseBody Going to buffer response body of larg= e or unknown size. Using getResponseBodyAsStream instead is recommended. [10/12/10 0:52:56:475 GMT] 00000164 SystemOut O - 302 Found

Found

The document has moved here.

Additionally, a 302 Found error was encountered while trying to use an ErrorDocument to handle the re= quest. [10/12/10 0:52:56:475 GMT] 00000164 SystemOut O - Redirection encounter= ed:302 [10/12/10 0:52:56:476 GMT] 00000164 SystemOut O - Redirect attempt: 1 [10/12/10 0:52:56:476 GMT] 00000164 SystemOut O - Setting credentials f= or redirect. [10/12/10 0:52:56:477 GMT] 00000164 SystemOut O - Credentials set [10/12/10 0:52:56:478 GMT] 00000164 SystemOut O - Preemptive Authentica= tion set [10/12/10 0:52:56:478 GMT] 00000164 SystemOut O - Redirecting to locati= on:https://159.37.35.247:443/?&STCO=3D1TLOxTpeXQHAAAEqsStY&STCOEND [10/12/10 0:52:56:561 GMT] 00000164 HttpMethodDir I org.apache.commons.http= client.HttpMethodDirector isRedirectNeeded Redirect requested but followRed= irects is disabled [10/12/10 0:52:56:568 GMT] 00000164 SystemOut O - HTTPS RedirectRespons= eStatusCode=3D302 [10/12/10 0:52:56:568 GMT] 00000164 SystemOut O - HTTP/1.1 302 Found [10/12/10 0:52:56:569 GMT] 00000164 HttpMethodBas W org.apache.commons.http= client.HttpMethodBase getResponseBody Going to buffer response body of larg= e or unknown size. Using getResponseBodyAsStream instead is recommended. [10/12/10 0:52:56:584 GMT] 00000164 SystemOut O - 302 Found

Found

The document has moved here.

Additionally, a 302 Found error was encountered while trying to use an ErrorDocument to handle the re= quest. [10/12/10 0:52:56:585 GMT] 00000164 SystemOut O - Redirect attempt: 2 [10/12/10 0:52:56:585 GMT] 00000164 SystemOut O - Setting credentials f= or redirect. [10/12/10 0:52:56:586 GMT] 00000164 SystemOut O - Credentials set [10/12/10 0:52:56:586 GMT] 00000164 SystemOut O - Preemptive Authentica= tion set [10/12/10 0:52:56:587 GMT] 00000164 SystemOut O - Redirecting to locati= on:https://159.37.35.247/?&STCO=3D2TLOxTpeXQHAAAEqsStY&STCOEND [10/12/10 0:52:56:672 GMT] 00000164 HttpMethodDir I org.apache.commons.http= client.HttpMethodDirector isRedirectNeeded Redirect requested but followRed= irects is disabled [10/12/10 0:52:56:680 GMT] 00000164 SystemOut O - HTTPS RedirectRespons= eStatusCode=3D302 [10/12/10 0:52:56:681 GMT] 00000164 SystemOut O - HTTP/1.1 302 Found [10/12/10 0:52:56:681 GMT] 00000164 HttpMethodBas W org.apache.commons.http= client.HttpMethodBase getResponseBody Going to buffer response body of larg= e or unknown size. Using getResponseBodyAsStream instead is recommended. [10/12/10 0:52:56:696 GMT] 00000164 SystemOut O - 302 Found

Found

The document has moved here.

Additionally, a 302 Found error was encountered while trying to use an ErrorDocument to handle the re= quest. [10/12/10 0:52:56:696 GMT] 00000164 SystemOut O - Redirect attempt: 3 [10/12/10 0:52:56:697 GMT] 00000164 SystemOut O - Setting credentials f= or redirect. [10/12/10 0:52:56:698 GMT] 00000164 SystemOut O - Credentials set [10/12/10 0:52:56:698 GMT] 00000164 SystemOut O - Preemptive Authentica= tion set [10/12/10 0:52:56:698 GMT] 00000164 SystemOut O - Redirecting to locati= on:https://159.37.35.247/ [10/12/10 0:52:56:778 GMT] 00000164 AuthChallenge I org.apache.commons.http= client.auth.AuthChallengeProcessor selectAuthScheme Basic authentication sc= heme selected [10/12/10 0:52:56:786 GMT] 00000164 HttpMethodDir I org.apach= e.commons.httpclient.HttpMethodDirector processWWWAuthChallenge Failure aut= henticating with BASIC 'FileDriveWWW'@159.37.35.247:443 [10/12/10 0:52:56:792 GMT] 00000164 SystemOut O - HTTPS RedirectRespons= eStatusCode=3D401 [10/12/10 0:52:56:792 GMT] 00000164 SystemOut O - HTTP/1.1 401 Authoriz= ation Required [10/12/10 0:52:56:793 GMT] 00000164 HttpMethodBas W org.apache.commons.http= client.HttpMethodBase getResponseBody Going to buffer response body of larg= e or unknown size. Using getResponseBodyAsStream instead is recommended. [10/12/10 0:52:56:809 GMT] 00000164 SystemOut O - 401 Authorization Required

Authorization R= equired

This server could not verify that you are authorized to access= the document requested. Either you supplied the wrong credentials (e.g., = bad password), or your browser doesn't understand how to supply the credent= ials required.

[10/12/10 0:52:56:809 GMT] 00000164 SystemOut O - File upload via HTTPS= failed. Thanks Fayaz --------------------------------------------------------------------- To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org For additional commands, e-mail: httpclient-users-help@hc.apache.org