所以,我正在开发一个登录我学校成绩网站的应用程序,发送URL编码的用户名和密码表单。浏览器登录的表单数据是:
Database=10
LogOnDetails.UserName=yourusernamehere
LogOnDetails.Password=yourpasswordhere
我编辑了我的帖子以包含我的新代码,该代码使用了Apache的HttpClient。
Amit告诉我,代码200 OK是一个合适的响应,即使它在我的浏览器中返回302 FOUND。因此,我尝试在url网站重定向用户的get请求,但我只获取登录页面内容。
编辑:它有效。我不知道怎么做,但它有效。我提交了我的代码作为答案。我知道我很难到达这里,所以任何看到这个的人都欢迎自己复制我的代码。答案 0 :(得分:0)
code()仅返回HTTP RESPONSE CODE,200表示HTTP_OK,表示您的请求已成功传递。
我假设您期待“302 FOUND”作为api响应。然后你应该拨打body()
。
答案 1 :(得分:0)
所以我设法让它运转起来。 HttpRequest,HttpUrlConnection等都是一团糟。这完全适用于android。
public int login(){
String loginsite = "https://home-access.cfisd.net/HomeAccess/Account/LogOn";
String gradesite = "https://home-access.cfisd.net/HomeAccess/Classes/Classwork";
String stud_ID = encryptor.land("student_id");
String stud_pass = encryptor.land("student_password");
HttpResponse response;
int code =0;
if(stud_ID==null || stud_pass == null || stud_ID.length()<7 || stud_pass.length()<1)
return -1;
try{
HttpClient client = new DefaultHttpClient();
List<NameValuePair> nvp = new ArrayList<>();
nvp.add(new BasicNameValuePair("Database","10"));
nvp.add(new BasicNameValuePair("LogOnDetails.UserName",stud_ID));
nvp.add(new BasicNameValuePair("LogOnDetails.Password",stud_pass));
code = sendPost(loginsite,nvp,client);
System.out.println(getPageContent(gradesite,client));
}catch(Exception e){
e.printStackTrace();
return -2;
}
return code;
}
private int sendPost(String url, List<NameValuePair> postParams, HttpClient client)
throws Exception {
HttpPost post = new HttpPost(url);
post.setHeader("Accept-Language","en-US,en;q=0.8");
post.setHeader("Host", "home-access.cfisd.net");
post.setHeader("Origin", "https://home-access.cfisd.net");
post.setHeader("DNT","1");
post.setHeader("User-Agent", "Mozilla/5.0");
post.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
post.setHeader("cache-control","max-age=0");
post.setHeader("Connection", "keep-alive");
post.setHeader("Referer", "https://home-access.cfisd.net/HomeAccess/Account/LogOn?ReturnUrl=%2fHomeAccess%2f");
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
post.setEntity(new UrlEncodedFormEntity(postParams));
HttpResponse response = client.execute(post);
int responseCode = response.getStatusLine().getStatusCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + postParams);
System.out.println("Response Code : " + responseCode);
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line+"\n");
}
return responseCode;
}
protected String getPageContent(String url,HttpClient client) throws Exception {
HttpGet get = new HttpGet(url);
get.addHeader("accept-language","en-US,en;q=0.8");
get.addHeader("referer","https://home-access.cfisd.net/HomeAccess/Account/LogOn");
get.addHeader("DNT","1");
get.addHeader("connection","keep-alive");
get.addHeader("content-type","application/x-www-form-urlencoded");
get.addHeader("cache-control","max-age=0");
get.addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
get.addHeader("user-agent","Mozilla/5.0");
HttpResponse response = client.execute(get);
int responseCode = response.getStatusLine().getStatusCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line+"\n");
}
return result.toString();
}