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();
      }
  • 相关阅读:
    2018-10-20-WPF-通过位处理合并图片
    2019-2-11-WPF-获取应用的所有窗口
    2019-2-11-WPF-获取应用的所有窗口
    2018-8-10-WPF-如何在绑定失败异常
    2018-8-10-WPF-如何在绑定失败异常
    类和对象
    类和对象
    什么是可串行化MVCC
    LeetCode-环形链表|+环形链表||
    用js仿探探拖拽卡片的效果、飞卡片的效果,感觉挺酷,最后有美女看哦!
  • 原文地址:https://www.cnblogs.com/pan-my/p/14041866.html
Copyright © 2011-2022 走看看