HttpClient,如何在URI字符串中发送非法字符

时间:2012-03-29 18:49:35

标签: java httpclient

我想提出一些自动化测试来验证我的服务器处理无效请求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

有什么想法?

2 个答案:

答案 0 :(得分:2)

\我逃脱了。结果是一个标签。

网址只能保留可打印的US-ASCII代码。 其他代码有%符号

用于反斜杠

%5c =“\”

答案 1 :(得分:-1)

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

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