• post 发送方式


    /**

       * post 方式 解码

       */

       public static String getWebContentByPost(String urlString, String data,

            final String charset, int timeout) throws IOException {

          if (urlString == null || urlString.length() == 0) {

            return null;

          }

          urlString = (urlString.startsWith("http://") || urlString.startsWith("https://")) ? urlString : ("http://" + urlString).intern();

          URL url = new URL(urlString);

          System.out.println("url=="+url);

          HttpURLConnection connection = (HttpURLConnection) url.openConnection();

          // 设置是否向connection输出,因为这个是post请求,参数要放在 http正文内,因此需要设为true

          connection.setDoOutput(true);

          connection.setDoInput(true);

          connection.setRequestMethod("POST");

          // Post 请求不能使用缓存

          connection.setUseCaches(false);

          connection.setInstanceFollowRedirects(true);

          connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset="+charset+"");

          // 增加报头,模拟浏览器,防止屏蔽

          connection.setRequestProperty("User-Agent",

               "Mozilla/4.0 (compatible; MSIE 8.0; Windows vista)");

          // 只接受text/html类型,当然也可以接受图片,pdf,*/*任意

          connection.setRequestProperty("Accept", "*/*");// text/html

          connection.setConnectTimeout(timeout);

          connection.connect();

          DataOutputStream out = new DataOutputStream(connection

               .getOutputStream());

          byte[] content = data.getBytes(charset);// +URLEncoder.encode("中文 ",

          out.write(content);

          out.flush();

          out.close();

          try {

            // 必须写在发送数据的后面

            if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {

               return null;

            }

          } catch (IOException e) {

            e.printStackTrace();

            return null;

          }

          BufferedReader reader = new BufferedReader(new InputStreamReader(

               connection.getInputStream(), charset));

          String line;

          StringBuffer sb = new StringBuffer();

          while ((line = reader.readLine()) != null) {

            sb.append(line).append(" ");

          }

          if (reader != null) {

            reader.close();

          }

          if (connection != null) {

            connection.disconnect();

          }

          System.out.println(sb.toString());

          return URLDecoder.decode(sb.toString(), "utf-8");

       }

    public static String getUndecodeByPost(String urlString, String data,

               final String charset, int timeout) throws IOException {

            if (urlString == null || urlString.length() == 0) {

               return null;

            }

            urlString = (urlString.startsWith("http://") || urlString.startsWith("https://")) ? urlString : ("http://" + urlString).intern();

            URL url = new URL(urlString);

            System.out.println("url=="+url);

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            // 设置是否向connection输出,因为这个是post请求,参数要放在 http正文内,因此需要设为true

            connection.setDoOutput(true);

            connection.setDoInput(true);

            connection.setRequestMethod("POST");

            // Post 请求不能使用缓存

            connection.setUseCaches(false);

            connection.setInstanceFollowRedirects(true);

             connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset="+charset+"");

            // 增加报头,模拟浏览器,防止屏蔽

            connection.setRequestProperty("User-Agent",

                  "Mozilla/4.0 (compatible; MSIE 8.0; Windows vista)");

            // 只接受text/html类型,当然也可以接受图片,pdf,*/*任意

            connection.setRequestProperty("Accept", "*/*");// text/html

            connection.setRequestProperty("Content-Length", "*/*");// text/html

           

            connection.setConnectTimeout(timeout);

            connection.connect();

            DataOutputStream out = new DataOutputStream(connection

                  .getOutputStream());

            byte[] content = data.getBytes(charset);// +URLEncoder.encode("中文 ",

            out.write(content);

            out.flush();

            out.close();

            try {

               // 必须写在发送数据的后面

               if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {

                  return null;

               }

            } catch (IOException e) {

               e.printStackTrace();

               return null;

            }

            BufferedReader reader = new BufferedReader(new InputStreamReader(

                  connection.getInputStream(), charset));

            String line;

            StringBuffer sb = new StringBuffer();

            while ((line = reader.readLine()) != null) {

               sb.append(line).append(" ");

            }

            if (reader != null) {

               reader.close();

            }

            if (connection != null) {

               connection.disconnect();

            }

            return sb.toString();

          }

  • 相关阅读:
    Winfrom 减少控件重绘闪烁的方法
    Blazor client-side Preview 预览版 如何调试 Debug
    实用,Windows后台守护进程iNeuDaemon发布。Linux操作系统下使用使用supervisor
    RabbitMQ消息队列之Windows下安装和部署(一)
    Angularjs接收服务端的布尔值
    python基础教程:浅谈python for循环的巧妙运用(迭代、列表生成式)
    Excel合并数据查找函数VLOOKUP()一直显示最后一行数据或者一直报错的解决方法
    RHCE(一)NFS服务详解——搭建与配置
    [WPF 自定义控件]在MenuItem上使用RadioButton
    EF CORE中复杂类型的映射
  • 原文地址:https://www.cnblogs.com/chinaifae/p/10399272.html
Copyright © 2020-2023  润新知