java - Android使用HttpURLConnection简单的获取百度的首页源码失败?

 尼一奥鸟 发布于 2022-10-26 17:48

就是想很简单的获取百度的源码,开了一个子线程,使用HttpURLConnection,却不知到为什么一直获取失败。求高手告之原因。

public class MainActivity extends Activity implements View.OnClickListener
{

    public static final int SHOW_RESPONSE = 0;

    private Button sendRequest;

    private TextView responseText;

    private Handler handler = new Handler() {

        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SHOW_RESPONSE:
                    String response = (String) msg.obj;
                    // 在这里进行UI操作,将结果显示到界面上
                    responseText.setText(response);
            }
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.web_layout);
        sendRequest = (Button) findViewById(R.id.send_request);
        responseText = (TextView) findViewById(R.id.response_text);
        sendRequest.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.send_request) {
            sendRequestWithHttpURLConnection();
        }

    }



    private void sendRequestWithHttpURLConnection() {
        // 开启线程来发起网络请求
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection = null;
                try {
                    URL url = new URL("http://www.baidu.com/");
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(8000);
                    connection.setReadTimeout(8000);
                    connection.setDoInput(true);
                    connection.setDoOutput(true);
                    if(connection.getResponseCode()!=200)
                    {
                        Log.d("haha", "code is "+connection.getResponseCode());
                    }
                    InputStream in = connection.getInputStream();
                    // 下面对获取到的输入流进行读取
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(in));
                    StringBuilder response = new StringBuilder();

                    String line;
                    while ((line = reader.readLine()) != null) {
                        response.append(line);
                    }
                    Message message = new Message();
                    message.what = SHOW_RESPONSE;
                    // 将服务器返回的结果存放到Message中
                    message.obj = response.toString();
                    handler.sendMessage(message);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (connection != null) {
                        connection.disconnect();
                    }
                }
            }
        }).start();
    }


}

布局代码:




    

log里没有任何异常信息报出,url的返回码为302.
真的很疑惑,因为代码检查过很多次了觉得没有问题,求助!!!!!

2 个回答
  • 楼上说的对。
    所以你需要做两件事:

    1. 替换url为:https://www.baidu.com

    2. 替换URLConnection,HttpURLConnection 替换为 HttpsURLConnection.

    2022-11-12 01:42 回答
  • 这段代码本身没什么问题,302也已经提示你问题所在了,你访问的URL地址有问题,百度现在是全站https,所以你应该使用https://www.baidu.com来进行测试,但http://www.baidu.com在浏览器上又能访问到页面,是什么问题呢?这是因为百度做了跳转,自动给你跳转到https://www.baidu.com上,这个跳转由一个302页面来进行,动作很快,基本察觉不到。这就是为什么你得到302而不是200的原因。

    2022-11-12 01:42 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有