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;

}

相关推荐
悟乙己2 分钟前
使用 Python 中的强化学习最大化简单 RAG 性能
开发语言·python·agent·rag·n8n
bug攻城狮4 分钟前
Spring Boot Banner
java·spring boot·后端
max5006007 分钟前
图像处理:实现多图点重叠效果
开发语言·图像处理·人工智能·python·深度学习·音视频
黑马源码库miui5208624 分钟前
JAVA同城打车小程序APP打车顺风车滴滴车跑腿源码微信小程序打车源码
java·微信·微信小程序·小程序·uni-app
MadPrinter30 分钟前
SpringBoot学习日记 Day11:博客系统核心功能深度开发
java·spring boot·后端·学习·spring·mybatis
淦出一番成就33 分钟前
Java反序列化接收多种格式日期-JsonDeserialize
java·后端
Java中文社群35 分钟前
Hutool被卖半年多了,现状是逆袭还是沉寂?
java·后端
RickyWasYoung1 小时前
【代码】matlab-遗传算法工具箱
开发语言·matlab
爱吃苹果的日记本1 小时前
开学第一课
java
渣哥2 小时前
Java 集合框架详解:常见集合类及分类方式
java