登录后从网站解析数据(通过POST发送凭据)

时间:2018-07-10 15:52:24

标签: android parsing post cookies httpsurlconnection

我正在尝试从需要凭证的网站检索数据。 我成功连接了正确的用户名和密码,并获得了 302 responseCode,但是当我使用BufferedReader时,我得到的是这样的东西:

<html><head><title>Object moved</title></head><body>
    <h2>Object moved to <a href="//">here</a>.</h2>
    </body></html>

顺便说一句,我想问一下我是否正确设置了cookie,因为我想从子页面中检索数据,并且我认为我可能会丢失会话

这是我的 MainActivity

public class MainActivity extends AppCompatActivity
{

   private String userName;
   private String password;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EditText userN = findViewById(R.id.userN);
        EditText passw = findViewById(R.id.Passwd);

        // uprawnienie internetowe
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

    }

    public void Submit(View view) throws IOException {
        EditText userN = findViewById(R.id.userN);
        userName = userN.getText().toString();
        EditText passw = findViewById(R.id.Passwd);
        password = passw.getText().toString();


        authenticationProcess auth = new authenticationProcess();

        if (!auth.verificationTry(userName,password))
        {
            Toast.makeText(MainActivity.this, "Nieprawidłowy login lub hasło!", Toast.LENGTH_SHORT).show();
        }
        else
        {
           Toast.makeText(MainActivity.this, "Udana próba logowania!",Toast.LENGTH_LONG).show();
        }
    }
}

以及我在MainActivity中使用的类 authenticationProces

   public class authenticationProcess extends AppCompatActivity {
    final String USER_AGENT = "Mozilla/5.0";
    boolean isConnected = false;

    public boolean verificationTry(String username, String password) throws IOException {
        //Tworzenie clienta
        URL url = new URL("https://gpt.ncplus.pl/Account/Login?ReturnUrl=%2f/");

        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setReadTimeout(10000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Connection", "keep-alive");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Accept-Charset", "utf-8");
     //   conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        conn.setRequestProperty("User-Agent", USER_AGENT);
        conn.setRequestProperty("Cookie", "");
        conn.setDoOutput(true);
        conn.setDoInput(true);

        //Do poprawy polaczenia ssl
        System.setProperty("jsse.enableSNIExtension", "false");

        //Zapisywanie ciastek
        CookieManager cookieManager = new CookieManager();

        CookieHandler.setDefault(cookieManager);


        //Wysylania zapytan ia POST
        Uri.Builder builder = new Uri.Builder()
                .appendQueryParameter("UserName", username)
                .appendQueryParameter("Password", password);
        String query = builder.build().getEncodedQuery();


        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(os, "UTF-8"));
        writer.write(query);
        writer.flush();
        writer.close();
        os.close();
        int responseCode = conn.getResponseCode();
        Log.e("code", String.valueOf(responseCode));

        conn.connect();

        //Read after connect
        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line + "\n");
        }
        br.close();


        if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP || responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_SEE_OTHER) {
            isConnected = true;
        } else {
            isConnected = false;
        }


        return (isConnected);
    }
}

编辑:我使用PC程序Postman,并且使用“不进行身份验证”登录时没有问题。仍然不知道为什么在Android上不起作用。

0 个答案:

没有答案