码迷,mamicode.com
首页 > Web开发 > 详细

在后台程序中发送http请求并获取响应数据

时间:2017-12-08 20:43:47      阅读:391      评论:0      收藏:0      [点我收藏+]

标签:keep   tin   +=   nec   请求   属性   property   buffer   style   

一,在后台程序中发送http请求获取响应数据

  1)以 http://libs.baidu.com/jquery/2.0.0/jquery.min.js 为例

二,

  1)

     String result="";
        BufferedReader in = null;
        URL url = null;
        try {
            url = new URL("http://libs.baidu.com/jquery/2.0.0/jquery.min.js");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        //打开连接
        URLConnection connection = url.openConnection();
        
        // 设置通用的请求属性
        connection.setRequestProperty("accept", "*/*");
        connection.setRequestProperty("connection", "Keep-Alive");
        connection.setRequestProperty("user-agent",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        // 建立实际的连接
        connection.connect();
        
        // 定义 BufferedReader输入流来读取URL的响应
        in = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));
        //写出数据
        String line;
        while ((line = in.readLine()) != null) {
            result += line;
        }
        //关流
        in.close();
        System.out.println(result);

三,

技术分享图片

在后台程序中发送http请求并获取响应数据

标签:keep   tin   +=   nec   请求   属性   property   buffer   style   

原文地址:http://www.cnblogs.com/hi-feng/p/8006750.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!