如何获取HttpUrlConnection完整响应

huangapple go评论145阅读模式
英文:

How to get HttpUrlConnection full response

问题

在过去的几天里我阅读了一些资料取得了一些进展以下是我编写的代码

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

没有错误一切都正常运行但问题是 - 我构建了这段代码来测试是否可以登录我尝试登录的网站但我无法从中获取任何信息
在我按下按钮后似乎发生了某些事情我发送到我的UI线程的输入流给我这个 
"java.io.BufferedInputStream@afe19b8"
每次按下按钮后它都在改变
我尝试使用conn.getResponseMessage() 并通过handle发送它但它只显示 "OK"所以在那方面也没有运气

我正在寻找的是连接后的网页的源代码在发送了我的两个cookie后它将能够显示我是否已登录
英文:

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.

答案1

得分: 0

请参考以下主题,了解如何处理HTTP响应。

https://stackoverflow.com/questions/61306061/logging-into-a-website-using-android-app-java

英文:

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

https://stackoverflow.com/questions/61306061/logging-into-a-website-using-android-app-java

huangapple
  • 本文由 发表于 2020年5月31日 01:37:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/62106324.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定