加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java处理HTTP请求的相关代码

发布时间:2020-12-14 23:30:13 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

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操作辅助工具
 *
 * Get和Post两者在传递参数方式上有差异
 *
 * 1、Get: 把参数附加到url上,比如URL="http://www.abc.com?name=123&age=18"
 * 2、Post:请求的url不变,但在content中加入参数,如 content="name=123&age=18",然后把content加入到post中
 *
 */
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);//Let the run-time system (RTS) know that we want input
            connection.setDoInput(true);//we want to do output.
            connection.setRequestMethod("POST");           
            connection.setUseCaches(false);// Post 请求不能使用缓存
            connection.setInstanceFollowRedirects(true);
            connection.setRequestProperty("Content-Type",//Specify the header content type.
                    "application/x-www-form-urlencoded");
            connection.connect();
              
            out = new DataOutputStream(connection.getOutputStream()); // Send POST output.
              
            // DataOutputStream.writeBytes将字符串中的16位的unicode字符变为utf-8的字符形式写道流里
            //content = URLEncoder.encode(content,"utf-8");
              
            out.writeBytes(content);
              
            /**
             * 如果url中带有多个key-value参数对,则采用下面的方式写到content中
             * 正文内容其实跟get的URL中'?'后的参数字符串一致
             *
            String content =
                "name=" + URLEncoder.encode ("Hitesh Agrawal") +
                "&profession=" + URLEncoder.encode ("Software Engineer");
            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)[^>]*?>[sS]*?<[s]*?/[s]*?(script|style)[s]*?>";
           String regEx_onevent="on[^s]+=s*";
           String regEx_hrefjs="href=javascript:";
           String regEx_iframe="<[s]*?(iframe|frameset)[^>]*?>[sS]*?<[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;
        }
  
    public static void main(String[] args){
        HttpTool ht = new HttpTool();
        try {
            ht.sendGetData("http://www.baidu.com","");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读