• 向指定Url发送post请求


    /**
       * 向指定 URL 发送POST方法的请求
       *
       * @param url 发送请求的 URL
       * @param param 请求参数,json格式,请求参数应该是{name:value1,name2:value2}的形式。
       * @return 所代表远程资源的响应结果
       * @throws IOException
       */
      public static String sendPost(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        StringBuilder result = new StringBuilder();
        URLConnection conn = null;
        try {
          URL realUrl = new URL(url);
          // 打开和URL之间的连接
          conn = realUrl.openConnection();
          // 设置通用的请求属性
          conn.setRequestProperty("accept", "*/*");
          conn.setRequestProperty("Accept-Charset", "UTF-8");
          conn.setRequestProperty("contentType", "UTF-8");
          conn.setRequestProperty("Accept-Language", Locale.getDefault().toString());
          conn.setRequestProperty("Accept",
              "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
          conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
          // 发送POST请求必须设置如下两行
          conn.setConnectTimeout(30000);
          conn.setReadTimeout(120000);
          conn.setDoOutput(true);
          conn.setDoInput(true);
          // 获取URLConnection对象对应的输出流
          out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));
          // out = new PrintWriter(conn.getOutputStream());
          // 发送请求参数
          out.print(param);
          // flush输出流的缓冲
          out.flush();
          // 定义BufferedReader输入流来读取URL的响应
          InputStream stream = conn.getInputStream();
          in = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
          String line;
          while ((line = in.readLine()) != null) {
            result.append(line);
          }
        } catch (Exception e) {
          e.printStackTrace();
          try {
            in = new BufferedReader(new InputStreamReader(((HttpURLConnection) conn).getErrorStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
              result.append(line);
            }
          } catch (IOException e1) {
            e1.printStackTrace();
          }
    
        } finally {
          try {
            if (out != null) {
              out.close();
            }
            if (in != null) {
              in.close();
            }
          } catch (IOException ex) {
            ex.printStackTrace();
          }
        }
        return result.toString();
      }
  • 相关阅读:
    python eval()函数
    Linux利用Sysctl命令调整内核参数
    Cookie测试的测试点
    python脚本对 MySQL 数据库进行增删改查
    页面加载经历的过程【转载】
    【转载】shell基础知识
    windows下查看端口占用情况
    Python的hashlib提供了常见的摘要算法
    Python统计文件夹下各个子目录中的文件个数
    Python读取文件下的图片格式
  • 原文地址:https://www.cnblogs.com/pan-my/p/14041866.html
Copyright © 2020-2023  润新知