0

After doing some reading during the last couple of days I have been able to make some progress and here is the code that I have come up with:

MainActivity:

package com.example.appv_6;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    Handler h1;
    Thread t1;
    TextView Text;
    Button Butt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Text = (TextView) findViewById(R.id.tvText_1);
        Butt = (Button) findViewById(R.id.bButt_1);

        h1 = new Handler(Looper.getMainLooper()){
            @Override
            public void handleMessage(Message msg){
                Text.setText(msg.obj.toString());
            }
        };

        Butt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                t1 = new Thread(new HTTPRequest(h1));
                t1.start();
            }
        });
    }
}

HTTPRequest

package com.example.appv_6;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

public class HTTPRequest implements Runnable {

    Handler h2;

    public HTTPRequest(Handler h) {
        h2 = h;
    }

    @Override
    public void run() {
        try {
            URL url = new URL("my url");
            HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

            conn.setRequestMethod("POST");

            conn.setDoInput(true);
            conn.setDoOutput(true);

            OutputStream out;
            InputStream in;
            conn.setRequestProperty("accept","text/html");
            conn.setRequestProperty("Cookie","ulogin=111111");
            conn.setRequestProperty("Cookie","upassword=222555");

            out = new BufferedOutputStream(conn.getOutputStream());
            in = new BufferedInputStream(conn.getInputStream());
            //String response = conn.getResponseMessage();

            Message msg = Message.obtain();
            msg.obj = in;

            h2.sendMessage(msg);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();

        }
    }
}

No errors, everything runs just fine, but the problem is - I have built this code as a test if I can log in the website I am trying to log into, yet I am not able to get any information out of this. After I press the button, it seems like something is happening and the InputStream that I am sending to my UI thread is giving me this: "java.io.BufferedInputStream@afe19b8" and after each button press, it keeps changing. I tried using conn.getResponseMessage() and send it via the handle, but it just shows "OK", so no luck there as well.

What I am looking for is the source code of the webpage that I am connected to after sending two of my cookies which will be able to show if I have logged in or not.

Aman Chourasiya
  • 1,078
  • 1
  • 10
  • 23
Vitaliy-T
  • 733
  • 6
  • 23

1 Answers1

0

Refer to this topic for details on how to handle HTTP response.

Logging into a website using Android app (Java)

Vitaliy-T
  • 733
  • 6
  • 23