Java后台发送post/get请求,HttpClient 工具类

package com.wangyj.utils;

 
import java.io.BufferedReader;

import java.io.DataOutputStream;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

import java.util.regex.Pattern;

/**

 * Http工具类

 * @author wangyj

 * @date 2020年1月6日

 */

public class HttpTool {

    /**

     * GET请求数据

     * @param get_url url地址

     * @param content  key=value形式

     * @return 返回结果

     * @throws Exception

     */

    public String sendGetData(String get_url, String content) throws Exception {

        String result = "";

        URL getUrl = null;

        BufferedReader reader = null;

        String lines = "";

        HttpURLConnection connection = null;

        try {

            if (content != null && !content.equals(""))

                get_url = get_url + "?" + content;

                //get_url = get_url + "?" + URLEncoder.encode(content, "utf-8");

            getUrl = new URL(get_url);

            connection = (HttpURLConnection) getUrl.openConnection();

            connection.connect();

            // 取得输入流,并使用Reader读取

            reader = new BufferedReader(new InputStreamReader(connection

                    .getInputStream(), "utf-8"));// 设置编码

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

                result = result + lines;

            }
            return result;

        } catch (Exception e) {

            throw e;

        } finally {

            if (reader != null) {

                reader.close();

                reader = null;
            }
            connection.disconnect();
        }
    }

    /**

     * @param POST_URL url地址

     * @param content  key=value形式

     * @return 返回结果

     * @throws Exception

     */

    public String sendPostData(String POST_URL, String content)

            throws Exception {

        HttpURLConnection connection=null;

        DataOutputStream out=null;

        BufferedReader reader=null;

        String line = "";

        String result="";

        try {

            URL postUrl = new URL(POST_URL);

            connection= (HttpURLConnection) postUrl.openConnection();

            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");

            connection.connect();
            
            out = new DataOutputStream(connection.getOutputStream());

            //content = URLEncoder.encode(content, "utf-8");
            //将字符串中的16位的unicode字符以8位的字符形式写道流里
            //DataOutputStream.writeBytes  

            out.writeBytes(content);
            out.flush();

            out.close();

            //获取结果
            reader = new BufferedReader(new InputStreamReader(

                    connection.getInputStream(), "utf-8"));// 设置编码

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

                result=result+line;
            }       

            return result;

        } catch (Exception e) {

            throw e;

        }finally {
            if(out!=null){
                out.close();
                out=null;               
            }
            if(reader!=null){
                reader.close();
                reader=null;               
            }
            connection.disconnect();
        }
    }

    /*

     * 过滤掉html里不安全的标签,不允许用户输入这些标签

     */

    public static String htmlFilter(String inputString) {

        //return inputString;

          String htmlStr = inputString; // 含html标签的字符串

          String textStr = "";

          java.util.regex.Pattern p_script;

          java.util.regex.Matcher m_script;

          try {

           String regEx_script = "<[\\s]*?(script|style)[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?(script|style)[\\s]*?>"; 

           String regEx_onevent="on[^\\s]+=\\s*";

           String regEx_hrefjs="href=javascript:";

           String regEx_iframe="<[\\s]*?(iframe|frameset)[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?(iframe|frameset)[\\s]*?>";

           String regEx_link="<[\\s]*?link[^>]*?/>";  

           htmlStr = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE).matcher(htmlStr).replaceAll(""); 

           htmlStr=Pattern.compile(regEx_onevent, Pattern.CASE_INSENSITIVE).matcher(htmlStr).replaceAll("");

           htmlStr=Pattern.compile(regEx_hrefjs, Pattern.CASE_INSENSITIVE).matcher(htmlStr).replaceAll("");

           htmlStr=Pattern.compile(regEx_iframe, Pattern.CASE_INSENSITIVE).matcher(htmlStr).replaceAll("");

           htmlStr=Pattern.compile(regEx_link, Pattern.CASE_INSENSITIVE).matcher(htmlStr).replaceAll("");


          // p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);

          // m_html = p_html.matcher(htmlStr);

          // htmlStr = m_html.replaceAll(""); // 过滤html标签 

           textStr = htmlStr;

          } catch (Exception e) {

               System.err.println("Html2Text: " + e.getMessage());
          }

          return textStr;

     }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值