I need to access to the content of an external website that goes over https protocol. This website shows your classes schedule, and I want to parse this information to adapt it in my app.
Well, I was thinking about making a connection to the login page, using httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); where I put the username and password. And then, making another connection to the schedule page, as I should be logged in. But the second response shows the html from the login page, so I guess this won't work this way, and I will need to have some cookies that I'll have to pass through the second request to be able to access.
Here is the code I'm using:
public class ConexionIntranet extends AsyncTask<String, Void, String> {
private String sUserName = "username...";
private String sPassword = "password...";
protected String doInBackground(String... urls) {
try {
InputStream is = null;
HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
DefaultHttpClient client = new DefaultHttpClient();
SchemeRegistry registry = new SchemeRegistry();
SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
registry.register(new Scheme("https", socketFactory, 443));
SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());
// Set verifier
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
// Example send http request
final String url = urls[0];
HttpPost httpPost = new HttpPost(url);
HttpPost httpPost2 = new HttpPost("https://url...");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("user", sUserName));
nameValuePairs.add(new BasicNameValuePair("pass", sPassword));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpPost2.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e1) {
Log.d("datos", "Error: "+e1.getMessage());
}
HttpResponse res;
String result;
try {
res = httpClient.execute(httpPost);
Log.d("datos", "Respuesta post: "+res.toString());
res = httpClient.execute(httpPost2);
is=res.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
Log.d("datos", "Respuesta: "+result);
} catch (ClientProtocolException e) {
Log.d("datos", "Error: "+e.getMessage());
} catch (IOException e) {
Log.d("datos", "Error: "+e.getMessage());
}
return null;
} catch (Exception e) {
Log.d("datos", "Error: "+e.getMessage());
return null;
}
}}
Any idea about how to solve this? Maybe it's possible to access the content and log in at the same time in just one request?
Thank you in advance for your time!
Cheers.