I'm trying to login into a website using java and jsoup. But every time I execute my post request I get an IOExeption. The website is www.seriecanal.com. I would appreciate it if someone could review the html form and tell me if I am creating the name-value pairs correctly and if there are any other obvious mistakes with the login. Here's my code:
public HttpResponse postData()
{
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://seriecanal.com/index.php?page=member");
try
{
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
//Get value of input "Regresa_web" because it changes each time i access the site
String Codigo_Fuente ="";
URL url = new URL("http://seriecanal.com/");
URLConnection conn = url.openConnection();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = "";
while ((line = rd.readLine()) != null)
{
Codigo_Fuente= Codigo_Fuente+line;
}
Document Doc = Jsoup.parse(Codigo_Fuente);
Element Regresa_WEb = Doc.getElementById("regresa_web");
String Valor_Regresa_Web = Regresa_WEb.attr("value");
//create name value pairs of the form
nameValuePairs.add(new BasicNameValuePair("_UserName", "userontheweb"));
nameValuePairs.add(new BasicNameValuePair("_Pwd", "123456789"));
nameValuePairs.add(new BasicNameValuePair("btnLogin", "Ingresar"));
nameValuePairs.add(new BasicNameValuePair("_submit_check", "1"));
nameValuePairs.add(new BasicNameValuePair("remember", "ON"));
nameValuePairs.add(new BasicNameValuePair("regresa_web", Valor_Regresa_Web));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse Response = httpclient.execute(httppost);
return Response;
}
catch (ClientProtocolException e)
{
return null;
}
catch (IOException e)
{
Log.i("ERROR", e.toString());
e.printStackTrace();
return null;
}
}
Stack:
04-21 13:42:04.206: D/dalvikvm(324): GC freed 15163 objects / 721152 bytes in 153ms
04-21 13:42:08.026: I/Resources(324): Loaded time zone names for en_US in 1863ms.
04-21 13:42:23.816: I/global(324): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
04-21 13:42:25.696: D/dalvikvm(324): GC freed 21866 objects / 1346160 bytes in 168ms