中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Java處理HTTP請(qǐng)求的相關(guān)代碼

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用

可以用于處理HTTP GET請(qǐng)求,POST請(qǐng)求,過(guò)濾HTML代碼等

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兩者在傳遞參數(shù)方式上有差異
 *
 * 1、Get: 把參數(shù)附加到url上,比如URL="http://www.abc.com?name=123&age=18"
 * 2、Post:請(qǐng)求的url不變,但在content中加入?yún)?shù),如 content="name=123&age=18",然后把content加入到post中
 *
 */
public class HttpTool {
      
    /**
     * GET請(qǐng)求數(shù)據(jù)
     * @param get_url url地址
     * @param content  key=value形式
     * @return 返回結(jié)果
     * @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"));// 設(shè)置編碼
            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 返回結(jié)果
     * @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 請(qǐng)求不能使用緩存
            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字符變?yōu)閡tf-8的字符形式寫道流里
            //content = URLEncoder.encode(content, "utf-8");
              
            out.writeBytes(content);
              
            /**
             * 如果url中帶有多個(gè)key-value參數(shù)對(duì),則采用下面的方式寫到content中
             * 正文內(nèi)容其實(shí)跟get的URL中'?'后的參數(shù)字符串一致
             *
            String content =
                "name=" + URLEncoder.encode ("Hitesh Agrawal") +
                "&profession=" + URLEncoder.encode ("Software Engineer");
            out.flush();
            out.close(); */
              
            //獲取結(jié)果
            reader = new BufferedReader(new InputStreamReader(
                    connection.getInputStream(), "utf-8"));// 設(shè)置編碼
            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();
        }
    }
     
    /*
     * 過(guò)濾掉html里不安全的標(biāo)簽,不允許用戶輸入這些符號(hào)
     */
    public static String htmlFilter(String inputString) {
        //return inputString;
          String htmlStr = inputString; // 含html標(biāo)簽的字符串
          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(""); // 過(guò)濾html標(biāo)簽
  
           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();
        }
    }
}

標(biāo)簽: 安全 代碼

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:jquery 滾動(dòng)到底部時(shí)自動(dòng)加載更多

下一篇:JS實(shí)現(xiàn)漢字簡(jiǎn)體繁體轉(zhuǎn)換