HttpClient 实例模拟登录 查询数据

EncodingUtil   POST 中文编码问题


两种中文问题;

1.filed字段值的中文

2.file名的中文

对于第一种,参看StringPart;其源代码有这样一段:

    private byte[] getContent() {
        if (content == null) {
            content = EncodingUtil.getBytes(value, getCharSet());
        }
        return content;
    }
    
    protected void sendData(OutputStream out) throws IOException {
        LOG.trace("enter sendData(OutputStream)");
        out.write(getContent());
    }

可以看出在发送数据时其调用了EncodingUtil的getBytes方法(利用了你通过setCharSet设置的编码)

因此,只要你在代码中这样:

   StringPart part = new StringPart(name, value);

   part.setCharSet("GBK");

中文就没有问题了

对于第二种,参看FilePart;其源代码中有这样一段:

protected void sendDispositionHeader(OutputStream out) 
    throws IOException {
        LOG.trace("enter sendDispositionHeader(OutputStream out)");
        super.sendDispositionHeader(out);
        String filename = this.source.getFileName();
        if (filename != null) {
            out.write(FILE_NAME_BYTES);
            out.write(QUOTE_BYTES);
            out.write(EncodingUtil.getAsciiBytes(filename));
            out.write(QUOTE_BYTES);
        }
    }

可以看出在转换文件名时,用的方法是EncodingUtil.getAsciiBytes(),查看这个方法源码为data.getBytes("US-ASCII"),因此中文文件名必定乱码,不管你是否调用了setCharSet("GBK")方法。

解决很简单:

   out.write(EncodingUtil.getBytes(filename, getCharSet()));

看了网上好多文章,好多都说改EncodingUtil类,其实我觉得改FilePart更好一些


post 请求的参数必须完整 即使是空参数 也必须带到post

下面是我调试好的例子    后面继续做过滤了~~

        String url = ""; 
        PostMethod postMethod = new PostMethod(url);
        NameValuePair[] postData = new NameValuePair[7];
        postData[0] = new NameValuePair("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
        postData[1] = new NameValuePair("j_username","");
        postData[2] = new NameValuePair("username","");
        postData[3] = new NameValuePair("password","");
        postData[4] = new NameValuePair("j_password","");
        postData[5] = new NameValuePair("goodsCartIdList","");
        postData[6] = new NameValuePair("re_Url","");
        postMethod.addParameters(postData);
        postMethod.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY); 
        client.executeMethod(postMethod);
        System.out.println("******************************login******************************");
        Cookie[] cookies = client.getState().getCookies();
        client.getState().addCookies(cookies);
        postMethod.releaseConnection();  
        
        for (int i = 0; i < cookies.length; i++) {
//            System.out.println(" - " + cookies[i].toExternalForm());
//            System.out.println(" - domain=" + cookies[i].getDomain());
//            System.out.println(" - path=" + cookies[i].getPath());
        }
        
        
        System.out.println("******************************redirect******************************");
        String newUrl = "";
        int i=0;  
        for(Cookie c:cookies){  
            System.out.println(++i+":   "+c);  
        }
        client.getState().addCookies(cookies);
        postMethod.releaseConnection();  
        String newRequestBody = getRes(newUrl,cookies);
//        System.out.println(newRequestBody);
        
        //**第二次模拟post */
        PostMethod pm = new PostMethod("");
        NameValuePair[] pd = new NameValuePair[7];
        postData[0] = new NameValuePair("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
        postData[1] = new NameValuePair("autopayCustomers.codeDesc","");
        postData[2] = new NameValuePair("autopayPaymentFlow.customerNo","");
        postData[3] = new NameValuePair("autopayPaymentFlow.regionCode","");
        postData[4] = new NameValuePair("autopayPaymentFlow.thirdPartyCode","");
        postData[5] = new NameValuePair("province","江苏省");
        postData[6] = new NameValuePair("type","elec");
        pm.addParameters(pd);
        pm.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY); 
        client.executeMethod(pm);
        System.out.println("******************************2nd post******************************");
        Cookie[] ck = client.getState().getCookies();
        client.getState().addCookies(ck);
        pm.releaseConnection();  

你可能感兴趣的:(windows,String,Cookies,null,url,redirect)