• 向URL获取响应结果


      1 package com.tedu.szrx.util;
      2 
      3 import java.io.BufferedReader;
      4 import java.io.IOException;
      5 import java.io.InputStreamReader;
      6 import java.io.PrintWriter;
      7 import java.net.URL;
      8 import java.net.URLConnection;
      9 import java.util.List;
     10 import java.util.Map;
     11 
     12 public class HttpRequest {
     13     /**
     14      * 向指定URL发送GET方法的请求
     15      * 
     16      * @param url
     17      *            发送请求的URL
     18      * @param param
     19      *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
     20      * @return URL 所代表远程资源的响应结果
     21      */
     22     public static String sendGet(String url, String param) {
     23         String result = "";
     24         BufferedReader in = null;
     25         try {
     26             String urlNameString = url + "?" + param;
     27             URL realUrl = new URL(urlNameString);
     28             // 打开和URL之间的连接
     29             URLConnection connection = realUrl.openConnection();
     30             // 设置通用的请求属性
     31             connection.setRequestProperty("accept", "*/*");
     32             connection.setRequestProperty("connection", "Keep-Alive");
     33             connection.setRequestProperty("user-agent",
     34                     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
     35             // 建立实际的连接
     36             connection.connect();
     37             // 获取所有响应头字段
     38             Map<String, List<String>> map = connection.getHeaderFields();
     39             // 遍历所有的响应头字段
     40             for (String key : map.keySet()) {
     41                 System.out.println(key + "--->" + map.get(key));
     42             }
     43             // 定义 BufferedReader输入流来读取URL的响应
     44             in = new BufferedReader(new InputStreamReader(
     45                     connection.getInputStream()));
     46             String line;
     47             while ((line = in.readLine()) != null) {
     48                 result += line;
     49             }
     50         } catch (Exception e) {
     51             System.out.println("发送GET请求出现异常!" + e);
     52             e.printStackTrace();
     53         }
     54         // 使用finally块来关闭输入流
     55         finally {
     56             try {
     57                 if (in != null) {
     58                     in.close();
     59                 }
     60             } catch (Exception e2) {
     61                 e2.printStackTrace();
     62             }
     63         }
     64         return result;
     65     }
     66 
     67     /**
     68      * 向指定 URL 发送POST方法的请求
     69      * 
     70      * @param url
     71      *            发送请求的 URL
     72      * @param param
     73      *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
     74      * @return 所代表远程资源的响应结果
     75      */
     76     public static String sendPost(String url, String param) {
     77         PrintWriter out = null;
     78         BufferedReader in = null;
     79         String result = "";
     80         try {
     81             URL realUrl = new URL(url);
     82             // 打开和URL之间的连接
     83             URLConnection conn = realUrl.openConnection();
     84             // 设置通用的请求属性
     85             conn.setRequestProperty("accept", "*/*");
     86             conn.setRequestProperty("connection", "Keep-Alive");
     87             conn.setRequestProperty("user-agent",
     88                     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
     89             // 发送POST请求必须设置如下两行
     90             conn.setDoOutput(true);
     91             conn.setDoInput(true);
     92             // 获取URLConnection对象对应的输出流
     93             out = new PrintWriter(conn.getOutputStream());
     94             // 发送请求参数
     95             out.print(param);
     96             // flush输出流的缓冲
     97             out.flush();
     98             // 定义BufferedReader输入流来读取URL的响应
     99             in = new BufferedReader(
    100                     new InputStreamReader(conn.getInputStream()));
    101             String line;
    102             while ((line = in.readLine()) != null) {
    103                 result += line;
    104             }
    105         } catch (Exception e) {
    106             System.out.println("发送 POST 请求出现异常!"+e);
    107             e.printStackTrace();
    108         }
    109         //使用finally块来关闭输出流、输入流
    110         finally{
    111             try{
    112                 if(out!=null){
    113                     out.close();
    114                 }
    115                 if(in!=null){
    116                     in.close();
    117                 }
    118             }
    119             catch(IOException ex){
    120                 ex.printStackTrace();
    121             }
    122         }
    123         return result;
    124     }    
    125 }
    126 
    127  
  • 相关阅读:
    2018.12.1 区块链论文翻译
    2018.11.29 区块链论文翻译
    jshell 一个代码片段测试工具(jdk9后新增的功能)
    java 的var 定义变量有点鸡肋...
    小心Math.abs(-2147483648)的坑
    java获取同级目录下的文件
    java获取formdata里的所有参数
    No enclosing instance of type VolatleTest is accessible. Must qualify the allocation with an enclosing instance of type VolatleTest
    if else太多怎么代替,太难维护?可以使用spring-plugin 插件系统
    设计一个泛型的获取数组最大值的函数.并且这个方法只能接受Number的子类并且实现了Comparable接口
  • 原文地址:https://www.cnblogs.com/myknow/p/8192859.html
Copyright © 2020-2023  润新知