0

我正在尝试登录一个网站并使用 android 模拟器显示它的源代码,但我无法使其工作,但仅在基于 java 的情况下,它正在工作。

这是我的代码:

package auth.test;

import android.app.Activity;
import android.os.Bundle;
// used for interacting with user interface
import android.widget.TextView;
import android.widget.EditText;
// used for passing data 
// used for connectivity
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class AuthActivity extends Activity {
/** Called when the activity is first created. */

//Handler h;
private static URL URLObj;
private static URLConnection connect;
EditText eText;
TextView tView;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

   tView=(TextView)findViewById(R.id.textView1);
   initControls();
}

    public void initControls()
    {

        try {

            URLObj = new URL("http://students.usls.edu.ph");
            connect = URLObj.openConnection();
            connect.setDoOutput(true);  
        }
        catch (MalformedURLException ex) {
            tView.setText("The URL specified was unable to be parsed or uses an invalid protocol. Please try again.");
            //System.exit(1); 
        }
        catch (Exception ex) {
            tView.setText("An exception occurred. " + ex.getMessage());
            //System.exit(1);
        }


        try {

            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connect.getOutputStream()));
            writer.write("username=0920204&password=******");
            //writer.close();


            BufferedReader reader = new BufferedReader(new InputStreamReader(connect.getInputStream()));

            String lineRead="";


            while((lineRead=reader.readLine())!=null)
            {
                tView.append(lineRead + "\n");
            }


            reader.close();

        }
        catch (Exception ex) {
            tView.setText("There was an error reading or writing to the URL: " + ex.getMessage());
        }
    }
}

所以,不是显示主页的源代码,而是显示登录页面的源代码。所以我认为代码的 Bufferedwriter 部分有问题。

4

2 回答 2

1

首先,您应该隐藏用户名/密码或伪造它们。

URLConnection处理饼干非常糟糕。这就是您刚刚从登录页面获得结果的原因。Android从 API 1 开始就内置了Apache HttpClient ,所以你可以使用它。Cookie 管理得非常好:-)

而且,您得到的是从服务器响应的原始文本,而不是源代码。

于 2012-07-07T13:55:39.877 回答
0

表单的名称是,根据页面来源,login.cfm所以你应该在那里发布,你可以以此为例: http: //www.devx.com/tips/Tip/18188

于 2012-07-07T13:58:36.083 回答