I am trying to send a website my username or password using some jar file from Apache and trying to read everything from the website with the help of my method “loadpage”.
It doesn’t work. After I execute my method “loadpage”. I still get the Streams from the main page, which I don’t need to be logged in. I want to have Streams after i am logged in.
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 it 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());
//con.setRequestProperty("Cookie", "JSESSIONID="+getSid());
URLConnection con = url.openConnection();
//con.connect();
con.setRequestProperty("Cookie", "JSESSIONID=" + encoding);
con.connect();
}
My Method Loadpage: it works because I tried it with some other websites, which don’t need an 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();
}
Actuualy what i did was correct. I found the Solution just with luck.I don’t know why, but i had to write
in.readLine();one time before theto get the Streams after i was logged in. If i dont do this line before While.. i get the Streams before i was logged in, allthough i was logged in.