欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
HttpClient POST 的 UTF-8 編碼問(wèn)題

問(wèn)題分析

不過(guò)在實(shí)際使用中, 還是發(fā)現按照最基本的方式調用 HttpClient 時(shí), 并不支持 UTF-8 編碼, 在網(wǎng)絡(luò )上找過(guò)一些文章, 也不得要領(lǐng), 于是查看了 commons-httpClient3.0.1 的一些代碼, 首先在 PostMethod 中找到了 generateRequestEntity() 方法:
    /**
     * Generates a request entity from the post parameters, if present.   Calls
     * {@link EntityEnclosingMethod#generateRequestBody()} if parameters have not been set.
     *
     * @since 3.0
     */
    protected RequestEntity generateRequestEntity() {
        if (!this.params.isEmpty()) {
            // Use a ByteArrayRequestEntity instead of a StringRequestEntity.
            // This is to avoid potential encoding issues.   Form url encoded strings
            // are ASCII by definition but the content type may not be.   Treating the content
            // as bytes allows us to keep the current charset without worrying about how
            // this charset will effect the encoding of the form url encoded string.
            String content = EncodingUtil.formUrlEncode(getParameters(), getRequestCharSet());
            ByteArrayRequestEntity entity = new ByteArrayRequestEntity(
                EncodingUtil.getAsciiBytes(content),
                FORM_URL_ENCODED_CONTENT_TYPE
            );
            return entity;
        } else {
            return super.generateRequestEntity();
        }
    }

原來(lái)使用 NameValuePair 加入的 HTTP 請求的參數最終都會(huì )轉化為 RequestEntity 提交到 HTTP 服務(wù)器, 接著(zhù)在 PostMethod 的父類(lèi) EntityEnclosingMethod 中找到了如下的代碼:
    /**
     * Returns the request's charset.   The charset is parsed from the request entity's
     * content type, unless the content type header has been set manually.
     *
     * @see RequestEntity#getContentType()
     *
     * @since 3.0
     */
    public String getRequestCharSet() {
        if (getRequestHeader("Content-Type") == null) {
            // check the content type from request entity
            // We can't call getRequestEntity() since it will probably call
            // this method.
            if (this.requestEntity != null) {
                return getContentCharSet(
                    new Header("Content-Type", requestEntity.getContentType()));
            } else {
                return super.getRequestCharSet();
            }
        } else {
            return super.getRequestCharSet();
        }
    }


解決方案

從上面兩段代碼可以看出是 HttpClient 是如何依據 "Content-Type" 獲得請求的編碼(字符集), 而這個(gè)編碼又是如何應用到提交內容的編碼過(guò)程中去的. 按照這個(gè)原來(lái), 其實(shí)我們只需要重載 getRequestCharSet() 方法, 返回我們需要的編碼(字符集)名稱(chēng), 就可以解決 UTF-8 或者其它非默認編碼提交 POST 請求時(shí)的亂碼問(wèn)題了.

測試

首先在 Tomcat 的 ROOT WebApp 下部署一個(gè)頁(yè)面 test.jsp, 作為測試頁(yè)面, 主要代碼片段如下:
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ page session="false" %>
<%
request.setCharacterEncoding("UTF-8");
String val = request.getParameter("TEXT");
System.out.println(">>>> The result is " + val);
%>


接著(zhù)寫(xiě)一個(gè)測試類(lèi), 主要代碼如下:
    public static void main(String[] args) throws Exception, IOException {
        String url = "http://localhost:8080/test.jsp";
        PostMethod postMethod = new UTF8PostMethod(url);
        //填入各個(gè)表單域的值
        NameValuePair[] data = {
                new NameValuePair("TEXT", "中文"),
        };
        //將表單的值放入postMethod中
        postMethod.setRequestBody(data);
        //執行postMethod
        HttpClient httpClient= new HttpClient();
        httpClient.executeMethod(postMethod);
    }
    
    //Inner class for UTF-8 support
    public static class UTF8PostMethod extends PostMethod{
        public UTF8PostMethod(String url){
            super(url);
        }
        @Override
        public String getRequestCharSet() {
            //return super.getRequestCharSet();
            return "UTF-8";
        }
    }


運行這個(gè)測試程序, 在 Tomcat 的后臺輸出中可以正確打印出 ">>>> The result is 中文" .
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
HttpClient PostMethod 傳遞json
BlogJava - lostfire - httpclient 3.0初步研究
HttpClient POST的中文編碼問(wèn)題
HttpClient對URL編碼的處理方式解惑! - Java - JavaEye論壇
使用HttpClient模擬網(wǎng)站登陸sina郵箱(保持遠程服務(wù)器session和cookie) (轉載) - 海浪 - 博客園
使用HttpClient+Regex抓取第三方網(wǎng)站信息(三)
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久