Link to home
Start Free TrialLog in
Avatar of lomidien
lomidien

asked on

Using Authenticator & httpClient

Here is a snipet of code that I'm using to authenticate to our corporate proxy server.  I can retrieve pages no problem, but I would like to incorporate this concept into my other project which utilized httpclient to log into a web-mail server programatically and check for new messages.  My question is how to use httpclient in this context because the below snipet is using HttpURLConnection.

try{
            Properties systemProperties = System.getProperties();

            String url = "http://eng.mail.ru/",
                         proxy = "proxyserverhere",
                         port = "80",
                         username = "usernamehere",
                         password = "passwordhere";

            Authenticator.setDefault(new SimpleAuthenticator(username, password));
            URL server = new URL(url);
            systemProperties.setProperty("http.proxyHost",proxy);
            systemProperties.setProperty("http.proxyPort",port);
            HttpURLConnection connection = (
            HttpURLConnection)server.openConnection();
            connection.connect();
            InputStream in = connection.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String line = new String();
            while((line=br.readLine())!=null)
            {
                System.out.println(line);
            }
           
        }
        catch(Exception e){e.printStackTrace();}


In my other program, I'm doing the following:

PostMethod post = new PostMethod("http://eng.mail.ru/");
int responseCode = httpclient.executeMethod(post);

how could these be combined????

Thanks,
David


Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Probably not that easily. I would configure the same parameters on the HttpClient
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lomidien
lomidien

ASKER

Yeah, you're probably right.  Doing this through the httpClient itself seems the way to go.

Thanks,
David
8-)