Java 如何使用ApacheHttpClient创建get请求;“坏的”;url中的符号(重新查询编码)?

Java 如何使用ApacheHttpClient创建get请求;“坏的”;url中的符号(重新查询编码)?,java,apache-httpclient-4.x,Java,Apache Httpclient 4.x,我正在尝试创建自动测试(java)。服务应该能够正确处理请求:返回json而不是java堆栈跟踪 在这个测试中,我遇到了类似#host/some/path/?param=% Ok,%是不正确的符号,应进行编码。当您是开发人员并创建发送请求的服务时。但这是一个考验 所以问题是:我如何使用url«原样»创建get响应,而不进行编码?目前我正在使用ApacheHttpClient(4.5.2) 目前我没有什么特别之处: public void fetchGet(String uri) { Ht

我正在尝试创建自动测试(java)。服务应该能够正确处理请求:返回json而不是java堆栈跟踪

在这个测试中,我遇到了类似#host/some/path/?param=%

Ok,%是不正确的符号,应进行编码。当您是开发人员并创建发送请求的服务时。但这是一个考验

所以问题是:我如何使用url«原样»创建get响应,而不进行编码?目前我正在使用ApacheHttpClient(4.5.2)

目前我没有什么特别之处:

public void fetchGet(String uri) {
    HttpGet getRequest = new HttpGet(uri);

    HttpClient client = HttpClientBuilder.create().build();

    try {
        response = client.execute(requestBase);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        requestBase.abort();
    }
}

使用
basichttrequest
而不是
HttpGet
来编写请求消息

CloseableHttpClient client = HttpClientBuilder.create().build();
BasicHttpRequest request = new BasicHttpRequest("GET", "all kind of c%.p");

try (CloseableHttpResponse response1 = client.execute(new HttpHost("www.google.com"), request)) {
    System.out.println(response1.getStatusLine());
    EntityUtils.consume(response1.getEntity());
}
此代码生成以下消息交换

[DEBUG] ProtocolExec - Unable to parse 'all kind of c%.p' as a valid URI; request URI and Host header may be inconsistent <java.lang.IllegalArgumentException: Illegal character in path at index 3: all kind of c%.p>java.lang.IllegalArgumentException: Illegal character in path at index 3: all kind of c%.p
    at java.net.URI.create(URI.java:852)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:120)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:118)
    at Testing.main(Testing.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.net.URISyntaxException: Illegal character in path at index 3: all kind of c%.p
    at java.net.URI$Parser.fail(URI.java:2848)
    at java.net.URI$Parser.checkChars(URI.java:3021)
    at java.net.URI$Parser.parseHierarchical(URI.java:3105)
    at java.net.URI$Parser.parse(URI.java:3063)
    at java.net.URI.<init>(URI.java:588)
    at java.net.URI.create(URI.java:850)
    ... 11 more

[DEBUG] RequestAddCookies - CookieSpec selected: default
[DEBUG] RequestAuthCache - Auth cache not set in the context
[DEBUG] PoolingHttpClientConnectionManager - Connection request: [route: {}->http://www.google.com:80][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
[DEBUG] PoolingHttpClientConnectionManager - Connection leased: [id: 0][route: {}->http://www.google.com:80][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20]
[DEBUG] MainClientExec - Opening connection {}->http://www.google.com:80
[DEBUG] DefaultHttpClientConnectionOperator - Connecting to www.google.com/216.58.213.100:80
[DEBUG] DefaultHttpClientConnectionOperator - Connection established xx.xx.xx.xx:36088<->216.58.213.100:80
[DEBUG] MainClientExec - Executing request GET all kind of c%.p HTTP/1.1
[DEBUG] MainClientExec - Target auth state: UNCHALLENGED
[DEBUG] MainClientExec - Proxy auth state: UNCHALLENGED
[DEBUG] headers - http-outgoing-0 >> GET all kind of c%.p HTTP/1.1
[DEBUG] headers - http-outgoing-0 >> Host: www.google.com
[DEBUG] headers - http-outgoing-0 >> Connection: Keep-Alive
[DEBUG] headers - http-outgoing-0 >> User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_112)
[DEBUG] headers - http-outgoing-0 >> Accept-Encoding: gzip,deflate
[DEBUG] headers - http-outgoing-0 << HTTP/1.1 400 Bad Request
[DEBUG] headers - http-outgoing-0 << Cache-Control: no-cache
[DEBUG] headers - http-outgoing-0 << Pragma: no-cache
[DEBUG] headers - http-outgoing-0 << Content-Type: text/html; charset=utf-8
[DEBUG] headers - http-outgoing-0 << Connection: close
[DEBUG] headers - http-outgoing-0 << Content-Length: 1904
HTTP/1.1 400 Bad Request
[DEBUG] DefaultManagedHttpClientConnection - http-outgoing-0: Close connection
[DEBUG] MainClientExec - Connection discarded
[DEBUG] PoolingHttpClientConnectionManager - Connection released: [id: 0][route: {}->http://www.google.com:80][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
[DEBUG]ProtocolExec-无法将“所有类型的c%.p”解析为有效的URI;请求URI和主机标头可能不一致java.lang.IllegalArgumentException:索引3处路径中的非法字符:所有类型的c%.p
在java.net.URI.create(URI.java:852)
位于org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:120)
位于org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
位于org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
位于org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
在org.apache.http.impl.client.CloseableHttpClient.execute上(CloseableHttpClient.java:118)
at Testing.main(Testing.java:15)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:498)
位于com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
原因:java.net.URISyntaxException:索引3处路径中的非法字符:所有类型的c%.p
在java.net.URI$Parser.fail(URI.java:2848)
位于java.net.URI$Parser.checkChars(URI.java:3021)
位于java.net.URI$Parser.parseHierarchy(URI.java:3105)
位于java.net.URI$Parser.parse(URI.java:3063)
位于java.net.URI。(URI.java:588)
创建(URI.java:850)
... 还有11个
[调试]RequestAddCookies-选择CookieSpec:默认值
[调试]RequestAuthCache-未在上下文中设置验证缓存
[调试]PoollightTPClientConnectionManager-连接请求:[路由:{}->http://www.google.com:80][保持活动状态的总数:0;分配的路由:0,共2条;分配的总数:0,共20条]
[调试]PoollightTPClientConnectionManager-租用的连接:[id:0][路由:{}->http://www.google.com:80][保持活动状态的总数:0;分配的路由:1/2;分配的总数:1/20]
[调试]MainClientExec-打开连接{}->http://www.google.com:80
[调试]默认HttpClientConnectionOperator-连接到www.google.com/216.58.213.100:80
[调试]DefaultHttpClientConnectionOperator-已建立连接xx.xx.xx.xx:36088216.58.213.100:80
[DEBUG]MainClientExec-执行请求获取所有类型的c%.p HTTP/1.1
[调试]MainClientExec-目标身份验证状态:未被质询
[调试]MainClientExec-代理身份验证状态:未被质询
[DEBUG]头-http-outgoing-0>>获取所有类型的c%.p http/1.1
[调试]标题-http-outing-0>>主机:www.google.com
[调试]标头-http-outgoing-0>>连接:保持活动状态
[DEBUG]headers-http-outing-0>>用户代理:Apache HttpClient/4.5.3(Java/1.8.0112)
[DEBUG]头文件-http-outgoing-0>>接受编码:gzip,deflate

[DEBUG]headers-http-outgoing-0使用
BasicHttpRequest
而不是
HttpGet
来编写请求消息

CloseableHttpClient client = HttpClientBuilder.create().build();
BasicHttpRequest request = new BasicHttpRequest("GET", "all kind of c%.p");

try (CloseableHttpResponse response1 = client.execute(new HttpHost("www.google.com"), request)) {
    System.out.println(response1.getStatusLine());
    EntityUtils.consume(response1.getEntity());
}
此代码生成以下消息交换

[DEBUG] ProtocolExec - Unable to parse 'all kind of c%.p' as a valid URI; request URI and Host header may be inconsistent <java.lang.IllegalArgumentException: Illegal character in path at index 3: all kind of c%.p>java.lang.IllegalArgumentException: Illegal character in path at index 3: all kind of c%.p
    at java.net.URI.create(URI.java:852)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:120)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:118)
    at Testing.main(Testing.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.net.URISyntaxException: Illegal character in path at index 3: all kind of c%.p
    at java.net.URI$Parser.fail(URI.java:2848)
    at java.net.URI$Parser.checkChars(URI.java:3021)
    at java.net.URI$Parser.parseHierarchical(URI.java:3105)
    at java.net.URI$Parser.parse(URI.java:3063)
    at java.net.URI.<init>(URI.java:588)
    at java.net.URI.create(URI.java:850)
    ... 11 more

[DEBUG] RequestAddCookies - CookieSpec selected: default
[DEBUG] RequestAuthCache - Auth cache not set in the context
[DEBUG] PoolingHttpClientConnectionManager - Connection request: [route: {}->http://www.google.com:80][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
[DEBUG] PoolingHttpClientConnectionManager - Connection leased: [id: 0][route: {}->http://www.google.com:80][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20]
[DEBUG] MainClientExec - Opening connection {}->http://www.google.com:80
[DEBUG] DefaultHttpClientConnectionOperator - Connecting to www.google.com/216.58.213.100:80
[DEBUG] DefaultHttpClientConnectionOperator - Connection established xx.xx.xx.xx:36088<->216.58.213.100:80
[DEBUG] MainClientExec - Executing request GET all kind of c%.p HTTP/1.1
[DEBUG] MainClientExec - Target auth state: UNCHALLENGED
[DEBUG] MainClientExec - Proxy auth state: UNCHALLENGED
[DEBUG] headers - http-outgoing-0 >> GET all kind of c%.p HTTP/1.1
[DEBUG] headers - http-outgoing-0 >> Host: www.google.com
[DEBUG] headers - http-outgoing-0 >> Connection: Keep-Alive
[DEBUG] headers - http-outgoing-0 >> User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_112)
[DEBUG] headers - http-outgoing-0 >> Accept-Encoding: gzip,deflate
[DEBUG] headers - http-outgoing-0 << HTTP/1.1 400 Bad Request
[DEBUG] headers - http-outgoing-0 << Cache-Control: no-cache
[DEBUG] headers - http-outgoing-0 << Pragma: no-cache
[DEBUG] headers - http-outgoing-0 << Content-Type: text/html; charset=utf-8
[DEBUG] headers - http-outgoing-0 << Connection: close
[DEBUG] headers - http-outgoing-0 << Content-Length: 1904
HTTP/1.1 400 Bad Request
[DEBUG] DefaultManagedHttpClientConnection - http-outgoing-0: Close connection
[DEBUG] MainClientExec - Connection discarded
[DEBUG] PoolingHttpClientConnectionManager - Connection released: [id: 0][route: {}->http://www.google.com:80][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
[DEBUG]ProtocolExec-无法将“所有类型的c%.p”解析为有效的URI;请求URI和主机标头可能不一致java.lang.IllegalArgumentException:索引3处路径中的非法字符:所有类型的c%.p
在java.net.URI.create(URI.java:852)
位于org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:120)
位于org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
位于org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
位于org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
在org.apache.http.impl.client.CloseableHttpClient.execute上(CloseableHttpClient.java:118)
at Testing.main(Testing.java:15)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:498)
位于com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
原因:java.net.URISyntaxException:索引3处路径中的非法字符:所有类型的c%.p
在java.net.URI$Parser.fail(URI.java:2848)
位于java.net.URI$Parser.checkChars(URI.java:3021)
位于java.net.URI$Parser.parseHierarchy(URI.java:3105)
位于java.net.URI$Parser.parse(URI.java:3063)
位于java.net.URI。(URI.java:588)
创建(URI.java:850)
... 还有11个
[调试]RequestAddCookies-选择CookieSpec:默认值
[调试]RequestAuthCache-未在上下文中设置验证缓存
[调试]PoollightTPClientConnectionManager-连接请求:[路由:{}->http://www.google.com:80][保持活动状态的总数:0;分配的路由:0,共2条;分配的总数:0,共20条]
[调试]PoollightTPClientConnectionManager-租用的连接:[id:0][路由:{}->http://www.google.com:80][保持活动状态的总数:0;分配的路由:1/2;分配的总数:1/20]
[调试]MainClientExec-打开连接{}->http://www.google.com:80
[调试]默认HttpClientConnectionOperator-连接到www.google.com/216.58.213.100:80
[调试]DefaultHttpClientConnectionOperator-已建立连接xx.xx.xx.xx:36088216.58.213.100:80
[调试]