I am trying to send a website to my username or password using some jar file from Apache and trying to read everything from the site using my "loadpage" method.
This does not work. After executing my "loadpage" method. I still get streams from the main page that I don’t need to enter into the system. I want to have threads after login.
public static void main(String[] args) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
httpclient.getCredentialsProvider().setCredentials(
new AuthScope("https://justawebsite", 8080),
new UsernamePasswordCredentials("username","password"));
HttpGet httpget = new HttpGet("https://justawebsite");
System.out.println("executing request" + httpget.getRequestLine());
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
EntityUtils.consume(entity);
} finally {
httpclient.getConnectionManager().shutdown();
}
}
I tried this also without the help of some jarfile from Apache.
public void LogIn(String url1) throws Exception{
URL url = new URL(url1);
String userPassword = "username"+":"+"password";
String encoding = new sun.misc.BASE64Encoder().encode (userPassword.getBytes());
URLConnection con = url.openConnection();
con.setRequestProperty("Cookie", "JSESSIONID=" + encoding);
con.connect();
}
My Method Loadpage: it works because I tried it with some other websites that don't need authentication.
public String loadPage (String url) throws Exception {
URLConnection con = new URL(url).openConnection();
StringBuilder buffer = new StringBuilder();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
while((line=in.readLine())!= null){
buffer.append(line);
}
in.close();
return buffer.toString();
}
source
share