当前位置: 首页 > news >正文

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

http://www.lryc.cn/news/150054.html

相关文章:

  • webRtc 示例
  • 【RabbitMQ】服务启动成功,无法访问localhost:15672(RabbitMQ Management)
  • 【操作记录】pytorch_geometric安装方法
  • EventSystem 事件系统
  • 2.2 Vector<T> 动态数组(模板语法)
  • dockerfile 例子(二)
  • openssh---Windows下git安装配置gitlab
  • vscode宏键绑定
  • 外贸企业如何借助CRM提升企业发展?
  • 初步了解ES
  • Linux基础(三)
  • python函数调用的四种方式
  • 如何将两个pdf合并成一个?pdf合并技巧分享
  • qt : day 3
  • flutter高德地图大头针
  • 【线性代数】矩阵求导的本质与分子布局、分母布局的本质(矩阵求导——本质篇)
  • 快速了解状态管理库Pinia及其使用方法
  • scratch绘制同心圆 2023年5月中国电子学会图形化编程 少儿编程 scratch编程等级考试四级真题和答案解析
  • 【LeetCode】3. 无重复字符的最长子串
  • 苹果macOS 14开发者预览版Beta 7发布 新增超过100款视频壁纸和屏保
  • Redis 7 第五讲 事务、管道、发布订阅 过渡篇
  • [SpringBoot3]视图技术Thymeleaf
  • wangluobiancheng
  • 大数据之hadoop入门
  • 浅谈多人游戏原理和简单实现。
  • 活动预告 | 龙智、紫龙游戏与JFrog专家将出席龙智DevSecOps研讨会,探讨企业大规模开发创新
  • 米每秒和千米每小时的换算
  • js实现图形验证码
  • 一句话画出动漫效果
  • 【openGauss2.1.0 TPC-C数据导入】