0

I'm trying to create auto test (java). Service should be able to handle request correctly: return json instead of java stack trace.

In this test i have cases like #host/some/path/?param=%

Ok, % is incorrect symbol and should be encoded. When you are developer and creating service that send requests. But this is a test.

So question is: how can i create get response with url «as is», without encoding? Currently i'm using apache httpclient (4.5.2)

Currently i have nothing really special:

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();
    }
}

1 Answer 1

2

Use BasicHttpRequest instead of HttpGet to compose a request message

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());
}

This code produces the following message exchange

[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]
Sign up to request clarification or add additional context in comments.

3 Comments

thanks BasicHttpRequest is what i was looking for. The only problem remain it cant be redirected is it? I mean let's say HttpHost host = new HttpHost("www.host.com"); String uri = "/some/path/?=%" in this case i will request www.host.com:80 (http) and, naturally, it is empty and return 301, redirect to www.host.com:443 (https). So the only way is to specify correct port/schema initially? HttpHost host = new HttpHost("www.host.com", 443, "https");
BasicHttpRequest can be redirected as long as the server responds with an absolute redirect location
the only thing i was able to imagine is to set redirection policy to no redirect. In case response status 300…302 create new request using url from response header. Is this what you been talking about? Or is there some solution out of box that i cant find?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.