Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

HttpClient and authentication

843807Nov 17 2008 — edited Nov 17 2008
Hello to all, and sorry about my bad english :P

I am using org.apache.commons.httpclient.HttpClient in my applet for file upload and I have problems with authentication when user connects to my site through proxy server and another problem is when my site (site where my applet is used, is protected with htaccess) Error for htaccess case is:
16.11.2008. 10.58.33 org.apache.commons.httpclient.auth.AuthChallengeProcessor selectAuthScheme
INFO: basic authentication scheme selected
16.11.2008. 10.58.33 org.apache.commons.httpclient.HttpMethodDirector processWWWAuthChallenge
INFO: No credentials available for BASIC ''@mzsite.com:80
and for proxy:
05.11.2008. 11.48.27 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: I/O exception (java.net.SocketException) caught when processing request: Network is unreachable: connect
05.11.2008. 11.48.27 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: Retrying request
I tried to solve htaccess with:
Credentials defaultcreds = new UsernamePasswordCredentials("username", "password");
client.getState().setCredentials(new AuthScope("myhost", 80, AuthScope.ANY_REALM), defaultcreds);
but it happends that upload is tried once, then when applet sees that need authentication, it authenticates and tries again, sucessfuly. This looks very silly since I have progress bar for upload and user can see what is happening. And even if I solve this (since this need to be independent component) I dont know credentials, since can be used on any site, so I need a runtime solution : java auth dialog...but then will probably have 2 prompts for credentials... :) one from the browser, and another from applet.
Before I tried HttpClient with URLConnection, everithing went automatically, and no proxy problems. I want that behaviour again, but with HttpClient benefits.

I found code for proxy detection:
public void detect(String location)
   {
      try {
        ProxyInfo info[] = ProxyService.getProxyInfo(new URL(location));
        if(info != null && info.length>0)
        {
          proxyHost = info[0].getHost();
          proxyPort = info[0].getPort();
          System.out.println("PROXY = " + proxyHost + ":" + proxyPort);
        }
      }catch (Exception ex) {
          System.err.println(
           "could not retrieve proxy configuration, attempting direct connection.");
        }
   }
but always get exception caught.

So, is there any solution to pass htaccess protection without double request and to get credentials from user, not to hardcode in my implementation, since this is my burning problem?
Solutions for proxy ptoblem are welcome as well.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 15 2008
Added on Nov 17 2008
1 comment
944 views