gpt4 book ai didi

java - 如何使用 apache httpclient 创建 get 请求,url 中带有 "bad"符号(需要编码)?

转载 作者:行者123 更新时间:2023-12-02 02:55:49 24 4
gpt4 key购买 nike

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

在这个测试中,我有类似 #host/some/path/?param=% 的情况

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

所以问题是:如何在不编码的情况下使用 url“按原样”创建获取响应?目前我正在使用 apache httpclient (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();
}
}

最佳答案

使用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]

关于java - 如何使用 apache httpclient 创建 get 请求,url 中带有 "bad"符号(需要编码)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43099353/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com