简体   繁体   English

如何获得 HttpUrlConnection 完整响应

[英]How to get HttpUrlConnection full response

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 HTTP请求

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.按下按钮后,似乎发生了什么事,我发送到 UI 线程的 InputStream 给了我这个:“java.io.BufferedInputStream@afe19b8”,每次按下按钮后,它都会不断变化。 I tried using conn.getResponseMessage() and send it via the handle, but it just shows "OK", so no luck there as well.我尝试使用 conn.getResponseMessage() 并通过句柄发送它,但它只显示“OK”,所以那里也没有运气。

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.我正在寻找的是我在发送两个 cookies 后连接到的网页的源代码,这将能够显示我是否已登录。

Refer to this topic for details on how to handle HTTP response.有关如何处理 HTTP 响应的详细信息,请参阅本主题。

Logging into a website using Android app (Java) 使用 Android 应用程序 (Java) 登录网站

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM