Java如何发起http的get请求的实现

加哥最近做第三方接口开发,对方提供的是get方式的http请求,下面加哥给大家进行了总结如何用java代码去发送http请求并获取结果。

下面是发送get请求的工具类

1.不要求携带token的方式

public static String getUrl(String tempurl,String bm) {
        String result="";
        try {
            URL url = new URL(tempurl);
            InputStream is = null;
            URLConnection con=url.openConnection();
            con.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            con.setConnectTimeout(120000);
            con.setReadTimeout(120000);
            con.connect();
            try {
                is = con.getInputStream();
                BufferedReader reader = null;
                try {
                    reader = new BufferedReader(new InputStreamReader(is,bm));
                    String s="";
                    String linesep = System.getProperty("line.separator");
                    while((s = reader.readLine())!=null){
                        result += s+linesep ;
                    }
                    reader.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    if (reader != null) {
                        try {
                            reader.close();
                        } catch (Exception e1) {
                        }
                    }
                }
                is.close();
            }catch (FileNotFoundException e2) {
                ;
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return result;
    }

下面是使用封装的好的请求工具类发送请求

public static void main(String[] args) {
        String str = "http://110.43.47.12:8089/system/sim?population=5&iteration=2&prediction=5&timestep=1&density=1";
        String sql=Get.getUrl(str,"utf-8");
        System.out.println(sql);
    }

注意请求发送后返回来的是JSON字符串,大家对其进行解析获取自己需要的数据即可。此外,还需注意第二个参数是编码格式。

2.要求携带token的方式

public static String getpage(String tempurl,String bm,String token ) {

String result="";

try {

URL url = new URL(tempurl);

InputStream is = null;

URLConnection con=url.openConnection();

con.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

con.setConnectTimeout(120000);

con.setReadTimeout(120000);

con.addRequestProperty("x-access-token",token);

con.connect();

try {

is = con.getInputStream();

BufferedReader reader = null;

try {

reader = new BufferedReader(new InputStreamReader(is,bm));

String s="";

String linesep = System.getProperty("line.separator");

while((s = reader.readLine())!=null){

result += s+linesep ;

}

reader.close();

} catch (Exception e) {

e.printStackTrace();

}finally {

if (reader != null) {

try {

reader.close();

} catch (Exception e1) {

}

}

}

is.close();

}catch (FileNotFoundException e2) {

;

}

} catch (Exception e1) {

e1.printStackTrace();

}

return result;

}

相关推荐
计算机小白一个9 分钟前
蓝桥杯 Java B 组之岛屿数量、二叉树路径和(区分DFS与回溯)
java·数据结构·算法·蓝桥杯
孤雪心殇10 分钟前
简单易懂,解析Go语言中的Map
开发语言·数据结构·后端·golang·go
庸俗今天不摸鱼22 分钟前
Canvas进阶-4、边界检测(流光,鼠标拖尾)
开发语言·前端·javascript·计算机外设
菠菠萝宝23 分钟前
【Java八股文】10-数据结构与算法面试篇
java·开发语言·面试·红黑树·跳表·排序·lru
奔跑吧邓邓子26 分钟前
【Python爬虫(36)】深挖多进程爬虫性能优化:从通信到负载均衡
开发语言·爬虫·python·性能优化·负载均衡·多进程
不会Hello World的小苗30 分钟前
Java——链表(LinkedList)
java·开发语言·链表
lsx20240642 分钟前
Perl 面向对象编程指南
开发语言
Allen Bright1 小时前
【Java基础-46.3】Java泛型通配符详解:解锁类型安全的灵活编程
java·开发语言
柃歌1 小时前
【UCB CS 61B SP24】Lecture 7 - Lists 4: Arrays and Lists学习笔记
java·数据结构·笔记·学习·算法
柃歌1 小时前
【UCB CS 61B SP24】Lecture 4 - Lists 2: SLLists学习笔记
java·数据结构·笔记·学习·算法