我试图通过POST发送用户凭证后从网站获取cookie请求它似乎不能在android中以这种方式工作。我做的事情不好吗?请帮忙。我在这里搜索了不同的帖子,但没有有用的答案。Android:HttpURLConnection无法正常工作
这很奇怪,它运行在桌面Java实现中,它工作的很完美,但在Android平台上崩溃。而且它完全相同的代码,特别是在调用HttpURLConnection.getHeaderFields()时,它也会发生在其他成员方法中。这是一个简单的代码,我不知道为什么地狱不工作。
DESKTOP CODE:这又只是在main()
HttpURLConnection connection = null;
OutputStream out = null;
try {
URL url = new URL("http://www.XXXXXXXX.php");
String charset = "UTF-8";
String postback = "1";
String user = "XXXXXXXXX";
String password = "XXXXXXXX";
String rememberme = "on";
String query = String.format("postback=%s&user=%s&password=%s&rememberme=%s"
, URLEncoder.encode(postback, charset)
, URLEncoder.encode(user,charset)
, URLEncoder.encode(password, charset)
, URLEncoder.encode(rememberme, charset));
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept-Charset", charset);
connection.setDoOutput(true);
connection.setFixedLengthStreamingMode(query.length());
out = connection.getOutputStream();
out.write(query.getBytes(charset));
if (connection.getHeaderFields() == null){
System.out.println("Header null");
}else{
for (String cookie: connection.getHeaderFields().get("Set-Cookie")){
System.out.println(cookie.split(";", 2)[0]);
}
}
} catch (IOException e){
e.printStackTrace();
} finally {
try { out.close();} catch (IOException e) { e.printStackTrace();}
connection.disconnect();
}
所以输出:
login_key=20ad8177db4eca3f057c14a64bafc2c9
FASID=cabf20cc471fcacacdc7dc7e83768880
track=30c8183e4ebbe8b3a57b583166326c77
client-data=%7B%22ism%22%3Afalse%2C%22showm%22%3Afalse%2C%22ts%22%3A1349189669%7D
ANDROID CODE:这又doInBackground的AsyncTask体内
HttpURLConnection connection = null;
OutputStream out = null;
try {
URL url = new URL("http://www.XXXXXXXXXXXXXX.php");
String charset = "UTF-8";
String postback = "1";
String user = "XXXXXXXXX";
String password = "XXXXXXXX";
String rememberme = "on";
String query = String.format("postback=%s&user=%s&password=%s&rememberme=%s"
, URLEncoder.encode(postback, charset)
, URLEncoder.encode(user,charset)
, URLEncoder.encode(password, charset)
, URLEncoder.encode(rememberme, charset));
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept-Charset", charset);
connection.setDoOutput(true);
connection.setFixedLengthStreamingMode(query.length());
out = connection.getOutputStream();
out.write(query.getBytes(charset));
if (connection.getHeaderFields() == null){
Log.v(TAG, "Header null");
}else{
for (String cookie: connection.getHeaderFields().get("Set-Cookie")){
Log.v(TAG, cookie.split(";", 2)[0]);
}
}
} catch (IOException e){
e.printStackTrace();
} finally {
try { out.close();} catch (IOException e) { e.printStackTrace();}
connection.disconnect();
}
并且在这里没有输出,看起来connection.getHeaderFields()不返回结果。它需要人至少30秒,以显示日志:
10-02 16:56:25.918: V/class com.giorgi.myproject.activities.HomeActivity(2596): Header null
测试的银河NEXUS
您可能需要刷新或关闭输出流? – njzk2
它在finally子句中关闭。我也尝试了冲洗,如你所说,但没有任何反应。 – giorgiline
我的意思是在读取标题之前 – njzk2