zoukankan      html  css  js  c++  java
  • 向指定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();
      }
  • 相关阅读:
    用户场景描述
    构建之法阅读笔记03
    冲刺记录(4.26)
    力扣-dp基础问题思维构建
    力扣-二叉树专题
    力扣-巧妙哈希
    力扣-双指针问题
    力扣-区间问题
    力扣-单调栈与单调队列问题
    力扣-股票买卖专题
  • 原文地址:https://www.cnblogs.com/pan-my/p/14041866.html
Copyright © 2011-2022 走看看