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;

}

相关推荐
老华带你飞1 小时前
医药垃圾分类管理系统|基于SSM+vue医药垃圾分类管理系统的系统设计与实现(源码+数据库+文档)
java·数据库·vue·毕业设计·论文·ssm·医药垃圾分类管理系统
程序员小续1 小时前
React 多个 HOC 嵌套太深,会带来哪些隐患?
java·前端·javascript·vue.js·python·react.js·webpack
mmmayang1 小时前
Golang 项目平滑重启
开发语言·后端·golang
褚翾澜2 小时前
Go语言的可选链
开发语言·后端·golang
rider1892 小时前
【9】搭建k8s集群系列(二进制部署)之安装work-node节点组件(kube-proxy)和网络组件calico
java·容器·kubernetes
ゞ 正在缓冲99%…2 小时前
leetcode274.H指数
java·算法·leetcode
程序员小杰@2 小时前
AI前端组件库Ant DesIgn X
开发语言·前端·人工智能
Aerkui3 小时前
Python高阶函数-eval深入解析
开发语言·python
小诸葛的博客3 小时前
client-go如何监听自定义资源
开发语言·后端·golang
入 梦皆星河3 小时前
go原理刨析之channel
开发语言·后端·golang