1

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
4

1 回答 1

1

如果您使用 fiddler 或 firebug,您可以询问发送的实际参数。当我这样做时,这就是我所看到的

_submit_check=1

btnLogin=Ingresar

密码=测试

记住=开

用户名=测试

看起来您发送了错误的参数。表单输入名称确实与发送的名称不同,例如_UserName,但您永远不知道javascript 是否绑定到该事件并且那里还有一些额外的魔法。结果我总是使用萤火虫/提琴手。

于 2012-04-21T20:37:56.537 回答