【问题标题】:HttpClient, How to send illegal characters in URI stringHttpClient,如何在 URI 字符串中发送非法字符
【发布时间】:2012-03-30 03:05:12
【问题描述】:

我想提出一些自动化测试来验证我的服务器对无效请求 URI 的处理。例如,测试人员可以在 windows 上使用 curl 将其作为他的 url 发送:

http://127.0.0.1/C:\an\incorrect\path\file.txt

我已经在服务器上处理了这个问题,但如果可能的话,我仍然想添加测试。尝试用 HttpClient 来做,但 HttpClient 不喜欢无效字符;)

// Run client
                String Url = targetHost + filePath + fileName;
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(Url);

                // run test
                InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(sourceDir.getAbsolutePath() + LusConstants.DIR_SEPERATOR + fileName),-1);
                reqEntity.setContentType("binary/octet-stream");
                reqEntity.setChunked(chunked);
                httppost.setEntity(reqEntity);
                httpclient.execute(httppost);

来自 HttpClient 的异常:

aused by: java.net.URISyntaxException: Illegal character in path at index 39: http://127.0.0.1:8080//a/deeper/path/an\incorrect\path\file.txt
    at java.net.URI$Parser.fail(Unknown Source)
    at java.net.URI$Parser.checkChars(Unknown Source)
    at java.net.URI$Parser.parseHierarchical(Unknown Source)
    at java.net.URI$Parser.parse(Unknown Source)
    at java.net.URI.<init>(Unknown Source)
    ... 4 more

有什么想法吗?

【问题讨论】:

    标签: java httpclient


    【解决方案1】:

    \i 转义。结果是一个标签。

    网址只能包含可打印的 US-ASCII 代码。 其他代码有 % 表示法

    用于反斜杠

    %5c = "\"

    【讨论】:

      【解决方案2】:

      也许您可以编写自己的原始 http 客户端,因此您的客户端没有 URL 验证。看看这个:

      http://code.joejag.com/2012/how-to-send-a-raw-http-request-via-java/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多