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;

}

相关推荐
Hello World......1 小时前
互联网大厂Java面试:从Spring到微服务的全面探讨
java·spring boot·spring cloud·微服务·面试·技术栈·互联网大厂
拾贰_C2 小时前
【SpringBoot】MyBatisPlus(MP | 分页查询操作
java·spring boot·后端·spring·maven·apache·intellij-idea
猛踹瘸子那条好腿の2 小时前
Spring-boot初次使用
java·springboot
shykevin4 小时前
python开发Streamable HTTP MCP应用
开发语言·网络·python·网络协议·http
我不是程序猿儿4 小时前
【C#】 lock 关键字
java·开发语言·c#
漫路在线4 小时前
JS逆向-某易云音乐下载器
开发语言·javascript·爬虫·python
小辉懂编程5 小时前
C语言:51单片机实现数码管依次循环显示【1~F】课堂练习
c语言·开发语言·51单片机
tmacfrank5 小时前
网络编程中的直接内存与零拷贝
java·linux·网络
醍醐三叶6 小时前
C++类与对象--2 对象的初始化和清理
开发语言·c++
weixin_472339466 小时前
Maven 下载安装与配置教程
java·maven