Android 解释HTTP POST响应
我正在尝试使用HTTPPOST在大学登录页面上提交用户信息。为此,我运行下面的postData()方法。代码发布凭据并读取响应。在代码下面,我展示了响应状态行和响应实体所显示的内容。(为了安全起见,我编辑了用户名和密码;)) 它似乎执行,因为我从网站上得到了一个响应,但我不知道如何解释它说什么,或者我需要做什么才能成功登录。该网站有一个用户名字段(id=user)、密码字段(id=pass)和登录按钮(id=submit)。这是一个安全的网站(https),但目前我没有做SSL连接,因为我只是想让这段代码正常工作 我面临的直接问题是:我是否可能需要SSL连接才能使其工作,以及Java是否是响应中提到的问题 再多一点背景。。。这是在服务中完成的,这是唯一的任务。如果Java是一个问题,那么在没有webview的情况下如何启用它Android 解释HTTP POST响应,android,http-post,Android,Http Post,我正在尝试使用HTTPPOST在大学登录页面上提交用户信息。为此,我运行下面的postData()方法。代码发布凭据并读取响应。在代码下面,我展示了响应状态行和响应实体所显示的内容。(为了安全起见,我编辑了用户名和密码;)) 它似乎执行,因为我从网站上得到了一个响应,但我不知道如何解释它说什么,或者我需要做什么才能成功登录。该网站有一个用户名字段(id=user)、密码字段(id=pass)和登录按钮(id=submit)。这是一个安全的网站(https),但目前我没有做SSL连接,因为我只是想
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.getEntity().getContent()
你提到这个链接了吗。我认为您需要执行setHeader(“Accept”,contentType);链接指向这个问题。我为你提到的setHeader编辑了这篇文章,但是没有用。
HTTP/1.1 200 OK
<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>
httppost.setHeader("Content-type", "application/x-www-form-urlencoded");
httppost.setHeader("Accept", "application/x-www-form-urlencoded");