1

我的程序会询问学生的用户名和密码,成功登录后,它会从主页的 HTML 源代码中获取学生的图片和姓名,并将其显示在另一个意图或活动上。

它运行良好,但我想查看另一个页面的 HTML 源代码,但我不知道如何。每次我创建另一个 httpclient 或 httpost 来访问另一个页面时,即使我已经登录,它也会显示登录页面的源代码。

这是主要代码:

public class TestingActivity extends Activity implements OnClickListener {
  String uname, pass, pic;
  EditText txtUname, txtPass;
  Button login;
  TextView result, tview2, tview3;
  String nameStartLine = " <td style=\"font:10px verdana; color:#312e25; text-align:right;\">Name:</td>";
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    login=(Button)findViewById(R.id.button1);
    result=(TextView)findViewById(R.id.textView1);
    tview2=(TextView)findViewById(R.id.textView2);
    tview3=(TextView)findViewById(R.id.textView3);
    txtUname=(EditText)findViewById(R.id.editText1);
    txtPass=(EditText)findViewById(R.id.editText2);

    login.setOnClickListener(this);
  }
  public void postLoginData() {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://students.usls.edu.ph/login.cfm");
    try {
      String username = txtUname.getText().toString();
      String password = txtPass.getText().toString();
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
      nameValuePairs.add(new BasicNameValuePair("username",username));
      nameValuePairs.add(new BasicNameValuePair("password",password));
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
      HttpResponse response = httpclient.execute(httppost);
      String str = inputStreamToString(response.getEntity().getContent()).toString();
      result.setText(str);
      Intent intent = new Intent(TestingActivity.this,panel_1.class);
      intent.putExtra("result",result.getText());
      pic = "http://teachers.usls.edu.ph/student_pics/" + txtUname.getText() + ".jpg";
      intent.putExtra("pic",pic);
      startActivity(intent);
    }
    catch (ClientProtocolException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  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) {
        if (line.equals(nameStartLine)) {
          line = rd.readLine();
          StringTokenizer st = new StringTokenizer(line, ">< ");
          String marker = st.nextToken();
          while(st.hasMoreTokens()) {
            if(marker.equals("strong")) {
              marker = st.nextToken();
              while(!(marker.equals("/strong"))) {
                total.append(marker + " ");
                marker = st.nextToken();
              }
            }
            marker = st.nextToken();
          }
        }
      }
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    // Return full string
    return total;
  }
  public void onClick(View view) {
    if(view == login){ postLoginData(); }
  }
}

这是第二个活动:

public class panel_1 extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main2);
    TextView result = (TextView)findViewById(R.id.txtresult);
    result.setText(""+getIntent().getExtras().getString("result"));
    //set position
    WebView myWebView = (WebView) findViewById(R.id.webView1);
    myWebView.loadUrl(getIntent().getExtras().getString("pic"));
  }
}

如您所见,程序将参数传递给http://students.usls.edu.ph/login.cfm. 成功登录后,它将获得主页的 HTML 源代码,我在其中获得学生的姓名和照片。那么如何将程序重定向到此链接并查看其 HTML 源代码,以便从中获取重要数据?我再次尝试使用httpclient,但它显示了登录页面的 HTML 源代码,或者http://students.usls.edu.ph/login.cfm即使我已经登录。

4

2 回答 2

0

在第二个活动中,您正在向服务器发出所有新请求,该服务器对身份验证凭据一无所知,因此它返回登录页面。因此,您还需要为第二个请求传递usernameand 。password

您需要使用postUrl

例如。

String url = getIntent().getExtras().getString("pic");
String postData = username=my_username&password=my_password;
webview.postUrl(url,EncodingUtils.getBytes(postData, "BASE64"));

我觉得你的代码在以下第一次活动的地方有问题

intent.putExtra("result",result.getText()); // result.getText().toString();
pic = "http://teachers.usls.edu.ph/student_pics/" + txtUname.getText() + ".jpg"; // txtUname.getText().toString();

而且,我建议您将WebViewClient用于一些高级功能。

于 2012-08-21T10:36:01.983 回答
0

我想我解决了。在http://students.usls.edu.ph/login.cfm成功登录后,我抓取了这样的 cookie

   List<Cookie> cookies = ((AbstractHttpClient) httpclient).getCookieStore().getCookies();
             if (cookies.isEmpty()) {
                Log.d("ERROR","no cookies received");
             } 
             else {
                for (int i = 0; i < cookies.size(); i++) {
                    if(cookies.get(i).getName().contentEquals("CFID")) {
                         CFID = cookies.get(i).getValue();
                     }
                    else if(cookies.get(i).getName().contentEquals("CFTOKEN")) {
                         CFTOKEN = cookies.get(i).getValue();
                     }

                }
             }

然后我使用这个链接http://students.usls.edu.ph/modules/modules.class_schedule.cfm调用了另一个 httppost来查看它的源代码。

             HttpPost httppost2 = new HttpPost("http://students.usls.edu.ph/modules/modules.class_schedule.cfm");
             List<NameValuePair> nameValuePairs2 = new ArrayList<NameValuePair>(2);
             nameValuePairs2.add(new BasicNameValuePair("CFID",CFID));
             nameValuePairs2.add(new BasicNameValuePair("CFTOKEN",CFTOKEN));
             httppost2.setEntity(new UrlEncodedFormEntity(nameValuePairs2));

最后我设法查看了http://students.usls.edu.ph/modules/modules.class_schedule.cfm的源代码。

于 2012-08-21T12:00:25.390 回答