首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

Http请求时的URL编码有关问题!中文字符是乱码

2012-10-15 
Http请求时的URL编码问题!!!中文字符是乱码。我要单元测试Servlet逻辑的正确性因此我预先构造了一个urlhtt

Http请求时的URL编码问题!!!中文字符是乱码。
我要单元测试Servlet逻辑的正确性
因此我预先构造了一个url
"http://localhost/donganshop/product/searcher?request=search&pagenumber=1&pagesize=5&name="+
URLEncoder.encode("电","utf8")+"%202000&number=001%20002&tlevel=0%202" ;
但中文字符“电”,在调试当中老是乱码,我的想法是客户端的信息编码及服务端的信息编码,都使用utf8.
由于这个乱码的原故,下面的这个测试总是过不了,所以也不能完成maven install,我使用断点来调式了我的所有与这个测试相关代码,判断出通不过的原因就是乱码引起的。而且url当中不使用中文字符是可以通过测试的。
关于这个单元测试,如何解决乱码问题呀。

Java code
public class TestProductSearcherServlet extends DongAnShopFixture{    private String preparedHttpReqUrl = "" ;     private ServletRunner runner = null ;    private ServletUnitClient unitClient = null ;    private ProductSearcherServlet servlet = null ;    private RequestHandler handler = null ;    private MockControl handlerMockControl = null ;        private List<Product> expectedProducts = null ;    private Request preparedRequest = null ;    private Response preparedResponse = null ;        public void setUp()throws Exception{        preparedHttpReqUrl =             "http://localhost/donganshop/product/searcher?request=search&pagenumber=1&pagesize=5&name="+            URLEncoder.encode("电","utf8")+"%202000&number=001%20002&tlevel=0%202" ;        runner = new ServletRunner(DongAnShopFixture.getWebContentPath("WEB-INF/web-test.xml"),"/donganshop");        runner.registerServlet("/product/searcher", ProductSearcherServlet.class.getName());        unitClient = runner.newClient();                handlerMockControl = MockControl.createControl(RequestHandler.class);        handler = (RequestHandler) handlerMockControl.getMock();            }    public void testSearcherWithFoundResult() throws Exception{        InvocationContext iCtx = unitClient.newInvocation(preparedHttpReqUrl);        servlet = (ProductSearcherServlet) iCtx.getServlet();        HttpServletRequest httpRequest = iCtx.getRequest();        httpRequest.setCharacterEncoding("utf8");//设置编码为utf8        HttpServletResponse httpResponse = iCtx.getResponse();                expectedProducts = new ArrayList<Product>();        expectedProducts.add(new Product("AMD平台电脑","002001","123456789"));        Map responseContext = new HashMap();        responseContext.put("products",expectedProducts);                preparedResponse = new SimpleResponse();        preparedResponse.setType(AbstractResponse.TYPE_INFO);        preparedResponse.setContext(responseContext);                preparedRequest = getFoundResultRequest();        handler.process(preparedRequest);        handlerMockControl.setReturnValue(preparedResponse);        handlerMockControl.replay();                servlet.putIntoRequestHandlers(AbstractRequest.NAME_SEARCH, handler);        servlet.init();        Response realResponse = servlet.processRequest(httpRequest, httpResponse);                assertEquals(AbstractResponse.TYPE_INFO,realResponse.getType());        List<Product> realProducts = (List<Product>) realResponse.getContext().get("products");        assertEquals(expectedProducts,realProducts);    }    private Request getFoundResultRequest(){        Request request = new SimpleRequest();        request.setName("search");        request.getContext().put("pageNumber",new Integer(1));        request.getContext().put("pageSize",new Integer(5));        Map<String,List> keywords = new HashMap<String,List>();        List<String> nameWords = new ArrayList<String>();        nameWords.add("电");        nameWords.add("2000");        List<String> numberWords = new ArrayList<String>();        numberWords.add("001");        numberWords.add("002");        List<Long> levelWords = new ArrayList<Long>();        levelWords.add(new Long(0));        levelWords.add(new Long(2));        keywords.put("name",nameWords);        keywords.put("number",numberWords);        keywords.put("typeLevel",levelWords);        request.getContext().put("keywords",keywords);        return request ;    }    public void tearDown(){        handlerMockControl.verify();    }} 



[解决办法]
服务器是啥?如果是Tomcat的话,默认URL编码是ISO8859-1,要修改下 server.xml 配置文件:

<Connector port="8080" protocol="HTTP/1.1" URIEncoding="UTF-8"
[解决办法]
你URLEncoder.encode编码两次就可以了
[解决办法]
探讨

服务器是啥?如果是Tomcat的话,默认URL编码是ISO8859-1,要修改下 server.xml 配置文件:

<Connector port="8080" protocol="HTTP/1.1" URIEncoding="UTF-8"

热点排行