登录后重定向到另一个URL

时间:2012-08-21 07:41:12

标签: android cookies

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

它运行良好,但我想查看另一个页面的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源代码,在那里我可以获得学生的姓名和图片。那么如何将程序重定向到this link并查看其HTML源代码,以便我可以从中获取重要数据?我再次尝试使用httpclient,但它显示了登录页面的HTML源代码或http://students.usls.edu.ph/login.cfm,即使我已经登录。

2 个答案:

答案 0 :(得分:0)

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

您需要使用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来获得一些高级功能。

答案 1 :(得分: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的源代码。