zoukankan      html  css  js  c++  java
  • URLConnection发送请求,并接收数据

    public static void main(String[] args) {
        BufferedReader in = null; //创建一个用来读取数据的,字符缓冲流
        String result=""; //创建用来接收数据的字符串
        try {
          URL url = new URL("http://api2.ofpay.com/querybigcard.do");//创建URL对象
          URLConnection con = url.openConnection(); //打开和URL之间的链接(打开连接)
          //设置通用的请求属性
          con.setRequestProperty("accept", "*/*");
          con.setRequestProperty("connection", "Keep-Alive");
          con.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
          //建立实际的链接
          con.connect();
          //定义BufferedReader字符缓冲流 ,来读取URL的响应
          in=new BufferedReader(new InputStreamReader(con.getInputStream()));
          //定义一个用来接收,一行行数据的字符串
          String line;
          while ((line = in.readLine())!= null) {
            result+=line;
          }
          System.out.println(result);
        } catch (Exception e) {
          e.printStackTrace();
        }

  • 相关阅读:
    迭代器、生成器、装饰器(转)
    Python小数据池
    接阿里云oss有感
    VSCode快捷键
    前端跨域调请求 nginx反向代理
    Git生成密钥
    【westorm系列之二】配置格式化
    钉钉安卓端无法渲染数据
    express 写接口
    js正则匹配身份证号 有坑
  • 原文地址:https://www.cnblogs.com/liuqu/p/8569644.html
Copyright © 2011-2022 走看看