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(n ew SimpleAuthenticator(userna me, password));
URL server = new URL(url);
systemProperties.setProper ty("http.p roxyHost", proxy);
systemProperties.setProper ty("http.p roxyPort", port);
HttpURLConnection connection = (
HttpURLConnection)server.o penConnect ion();
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(p ost);
how could these be combined????
Thanks,
David
try{
Properties systemProperties = System.getProperties();
String url = "http://eng.mail.ru/",
proxy = "proxyserverhere",
port = "80",
username = "usernamehere",
password = "passwordhere";
Authenticator.setDefault(n
URL server = new URL(url);
systemProperties.setProper
systemProperties.setProper
HttpURLConnection connection = (
HttpURLConnection)server.o
connection.connect();
InputStream in = connection.getInputStream(
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = new String();
while((line=br.readLine())
{
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(p
how could these be combined????
Thanks,
David
Probably not that easily. I would configure the same parameters on the HttpClient
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yeah, you're probably right. Doing this through the httpClient itself seems the way to go.
Thanks,
David
Thanks,
David
8-)