【问题标题】:How to get HttpUrlConnection full response如何获得 HttpUrlConnection 完整响应
【发布时间】:2020-05-30 17:37:15
【问题描述】:

在过去几天做了一些阅读之后,我已经取得了一些进展,这是我想出的代码:

主活动:

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();
            }
        });
    }
}

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();

        }
    }
}

没有错误,一切运行良好,但问题是 - 我已构建此代码作为测试是否可以登录我尝试登录的网站,但我无法从中获取任何信息. 在我按下按钮后,似乎发生了一些事情,我发送到我的 UI 线程的 InputStream 给了我这个: “java.io.BufferedInputStream@afe19b8” 每次按下按钮后,它都会不断变化。 我尝试使用 conn.getResponseMessage() 并通过句柄发送它,但它只显示“OK”,所以也没有运气。

我正在寻找的是我在发送两个 cookie 后连接到的网页的源代码,这些 cookie 将能够显示我是否已登录。

【问题讨论】:

标签: java android httpurlconnection


【解决方案1】:

有关如何处理 HTTP 响应的详细信息,请参阅本主题。

Logging into a website using Android app (Java)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-20
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    • 2020-05-15
    • 1970-01-01
    • 2017-09-16
    • 1970-01-01
    相关资源
    最近更新 更多