java调用http/https GET方法

时间:2025-03-19 09:35:15

java调用http/https GET方法

java调用api比较麻烦,get方法的调用封装好了,下面我们来获取一下百度的

代码如下:


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class TestGet {

    public static void main(String[] args) {
        System.out.println(get("/"));
    }

    public static String get(String url) {
        try {
            URL thisurl = new URL(url); // 把字符串转换为URL请求地址
            HttpURLConnection connection = (HttpURLConnection) thisurl
                    .openConnection();// 打开连接
            connection.connect();// 连接会话
            // 获取输入流
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    connection.getInputStream(), "UTF-8"));
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {// 循环读取流
                sb.append(line);
            }
            br.close();// 关闭流
            connection.disconnect();// 断开连接
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

结果

<!DOCTYPE html><!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=///img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=///s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn" autofocus></span> </form> </div> </div> <div id=u1> <a href= name=tj_trnews class=mnav>新闻</a> <a href=https:// name=tj_trhao123 class=mnav>hao123</a> <a href= name=tj_trmap class=mnav>地图</a> <a href= name=tj_trvideo class=mnav>视频</a> <a href= name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=/bdorz/?login&amp;tpl=mn&amp;u=http%3A%2F%%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>('<a href="/bdorz/?login&tpl=mn&u='+ encodeURIComponent(+ ( === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');                </script> <a href=///more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=>关于百度</a> <a href=>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=/duty/>使用百度前必读</a>&nbsp; <a href=/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=///img/> </p> </div> </div> </div> </body> </html>