0

我必须将 Jsoup 连接到一个 url,你可以在代码中找到它

public class MainActivity extends Activity {
private static final String LOG_TAG = "debugger";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    downloadThread.start(); 

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

Thread downloadThread = new Thread() {                     
    public void run() {                                    

        try {    
            Log.i(LOG_TAG,"Beginning");
            Response res = Jsoup
                    .connect("https://cas.scolastance.com/cas-auvergne/login?service=http://clg-cosne-d-allier.entauvergne.fr/sco/Modules/Accueil/Accueil.aspx")
                    .data("username", "myuser", "password", "mypass")
                    .data("submit", "SE CONNECTER") 
                    .method(Method.POST)
                    .execute();    
            Log.i(LOG_TAG,res.toString()); 

        } catch (IOException e) {                          
            e.printStackTrace();                           
        }                                                  
    }                                                      
};                                                         

}

回应是:

org.jsoup.helper.httpConnection$response@41577a10

这是否意味着连接正常?我是开发的初学者,对不起。谢谢回答

4

2 回答 2

0

尝试这个:

res.statusCode();

您将获得响应的状态码。

在这里查看更多信息:http: //jsoup.org/apidocs/org/jsoup/Connection.Response.html#statusCode ()

您也可以尝试显示res.body();

于 2013-01-16T19:37:30.917 回答
0

其实Jsoup是没有连接的。所以我尝试了这种方式:

            //First connection, to read the key
            Document doc = Jsoup.connect("https://cas.scolastance.com/cas-auvergne/login?service=http://clg-cosne-d-allier.entauvergne.fr/sco/Modules/Accueil/Accueil.aspx").get();
            String key ="";
            Element keyInput = doc.select("input[name=lt]").first();
            key = keyInput.attr("value");
            System.out.println("received value : " + key);

            //Second connection to post the data  
            Log.i(LOG_TAG,"Connecting ....");
            Response res = Jsoup
                    .connect("https://cas.scolastance.com/cas-auvergne/login?service=http://clg-cosne-d-allier.entauvergne.fr/sco/Modules/Accueil/Accueil.aspx")
                    .data("username", "myuser", "password", "mypass")

                    .data("lt", key) 
                    .data("_eventId", "submit") 
                    .data("submit", "SE CONNECTER") 
                    .method(Method.POST)
                    .execute(); 

            int statusCode = res.statusCode();
            String body = res.body().toString();
            System.out.println("received code : " + statusCode);
            System.out.println("received body : " + body);

但显示的主体仍然是“登录”主体,而不是“连接”主体。

于 2013-01-17T17:00:03.020 回答