我必须将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
这是否意味着连接正常? 抱歉,我是初学者。 谢谢你的回答
答案 0 :(得分:0)
试试这个:
res.statusCode();
您将获得响应的状态代码。
请点击此处:http://jsoup.org/apidocs/org/jsoup/Connection.Response.html#statusCode()
您也可以尝试展示res.body();
。
答案 1 :(得分: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);
但是显示的正文仍然是“登录”正文,而不是“连接”正文。