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

这篇具有很好参考价值的文章主要介绍了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;
    }文章来源地址https://www.toymoban.com/news/detail-682079.html

到了这里,关于Java如何发起http的get请求的实现的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • java基于RestTemplate的微服务发起http请求

    java基于RestTemplate的微服务发起http请求

    实现的效果

    2024年02月05日
    浏览(15)
  • uniapp发起post和get请求——this.$http.get 和 this.$http.post传参

    main.js按照内容自行修改

    2024年02月15日
    浏览(26)
  • 在 Linux 系统中,如何发起POST/GET请求

    在 Linux 系统中,可以使用命令行工具 `curl` 或者 `wget` 来发送 POST 请求。这两个工具都是非常常用的命令行工具,可以通过命令行直接发送 HTTP 请求。 1. 使用 `curl` 发送 POST 请求: 解释: - `-X POST`: 指定请求的方法为 POST。 - `-H \\\"Content-Type: application/json\\\"`: 指定请求头中的 Cont

    2024年02月15日
    浏览(10)
  • java调用http接口(get请求和post请求)

    1.http接口的格式如下: 图片选择失败,我只能把数据贴出来,如果有不懂的可以问我哈。 http://localhost:8881/department/getDepartmentList接口数据如下:(请求方式是GET) http://localhost:8881/department/getDataById?id=3接口数据如下:(请求方式是POST) 2.需要引入的包有: 3.实现方法如下:

    2024年02月13日
    浏览(8)
  • Java发送HTTP GET/POST请求

    在这篇文章中,将向你展示四种发送Http的GET/POST的例子,如下: 在Java11的java.net.http.*包中,有一个HttpClient类可以完成HTTP请求。 Java11HttpClientExample.java 本例使用HttpURLConnection(http)和HttpsURLConnection(https) HttpURLConnectionExample.java 使用Apache HttpClient完成HTTP请求的发送需要添加Maven依赖

    2024年02月13日
    浏览(13)
  • Java http GET POST 请求传参

    HTTP POST请求传参方式 方式一: 方式二 HTTP GET请求传参方式

    2024年02月15日
    浏览(14)
  • 【netty】java如何作为websocket客户端 对服务端发起请求

    【netty】java如何作为websocket客户端 对服务端发起请求

    是的 本文介绍java如何作为客户端 发起websocket请求 博主不做标题党 不会服务端客户端分不清就写个标题 乱写文章 为什么会使用java作为websocket客户端? 虽说websocket协议 本意是web与服务端之间的通讯协议,那假设有一天 我们的供应商 或者是甲方大爷 只提供了websocket接口呢?

    2024年02月05日
    浏览(8)
  • java http get post 和 发送json数据请求

    java http get post 和 发送json数据请求

    浏览器请求效果       main调用  

    2024年02月16日
    浏览(18)
  • java业务代码发送http请求(Post方式:请求参数为JSON格式;Get方式)

    实际开发中,可能需要发送http请求到第三方服务获取数据,于是就有以下应用: 依赖: 假设我需要在我的业务代码中调用该地址: url:http://xx.xx:xxxx/user/count 请求方法:post 内容类型:application/json 请求参数:id, username 返回参数:code 响应结果 int类型                  

    2024年02月12日
    浏览(17)
  • Java两种拼接http Get请求参数URL连接地址的方法

    Java两种拼接http Get请求参数URL连接地址的方法

    直接上代码吧,简单直接, 第一种,用String.format() 第二种:用Uri构造器 执行效果如下图:一模一样:

    2024年02月11日
    浏览(12)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包