我正在尝试使用HTTP POST在大学登录页面上提交用户信息。为此,我运行下面的postData()方法。代码发布凭据并读取响应。在代码下方,我已经显示了响应状态行和响应实体显示的内容。 (我编辑了安全性的用户名和密码;))。
它似乎是执行的,因为我收到了网站的回复,但我不知道如何解释它的说法或我需要做什么才能成功登录。有问题的网站有一个用户名字段(id = user),密码字段(id = pass)和登录按钮(id = submit)。它是一个安全的网站(https),但是现在我没有进行SSL连接,因为我只是想让这段代码正常工作。
我遇到的直接问题是:我是否可能需要SSL连接才能使其正常工作,并且Java是响应中提到的问题?
更多背景......这是在服务中完成的,这是唯一的任务。如果Java是问题,如何在没有webview的情况下启用它?
public void postData() {
// Create a new HttpClient and Post Header
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpClient httpclient = new DefaultHttpClient(params);
HttpPost httppost = new HttpPost("https://wiscmail.wisc.edu/login/");
TextView info = (TextView) findViewById(R.id.info);
EditText user = (EditText) findViewById(R.id.user);
EditText pass = (EditText) findViewById(R.id.pass);
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("user", user.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("pass", pass.getText().toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
Log.v(TAG, response.getStatusLine().toString());
info.setText(response.getStatusLine().toString());
HttpEntity responseEntity = response.getEntity();
Log.v(TAG, responseEntity.toString());
String response_string = inputStreamToString(response.getEntity().getContent()).toString();
Log.v(TAG, response_string);
} catch (ClientProtocolException e) {
Log.v(TAG, "client protocol exception");
info.setText("client protocol exception");
} catch (IOException e) {
Log.v(TAG, "IO exception");
info.setText("IO exception");
}
}
private StringBuilder inputStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
// Read response until the end
try {
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
// Return full string
return total;
}
response.getStatusLine
HTTP/1.1 200 OK
response.getEntity()。的getContent()
<html><head></head><body onLoad="document.relay.submit()">
<form method=post action="https://login.wisc.edu/?appurl=wiscmail.wisc.edu/login"
name=relay><input type=hidden name=pubcookie_g_req
value="b25lPXdpc2NtYWlsLndpc2MuZWR1JnR3bz1XaXNjTWFpbCtMb2dpbiZ0aHJlZT0xJmZvdXI9YTUmZml2ZT1QT1NUJnNpeD13aXNjbWFpbC53aXNjLmVkdSZzZXZlbj1MMnh2WjJsdUx3PT0mZWlnaHQ9Jmhvc3RuYW1lPXdpc2NtYWlsLndpc2MuZWR1Jm5pbmU9MSZmaWxlPSZyZWZlcmVyPShudWxsKSZzZXNzX3JlPTAmcHJlX3Nlc3NfdG9rPTM5NjgzODc5MSZmbGFnPTA=">
<input type=hidden name=post_stuff value="user=username&pass=password">
<input type=hidden name=relay_url value="https://wiscmail.wisc.edu/PubCookie.reply">
<noscript><p align=center>You do not have Javascript turned on, please click the
button to continue.<p align=center><input type=submit name=go value=Continue>
</noscript></form></html>
编辑: 根据建议我尝试将一个setHeader添加到http客户端,但我从网站获得了相同的响应。
httppost.setHeader("Content-type", "application/x-www-form-urlencoded");
httppost.setHeader("Accept", "application/x-www-form-urlencoded");