0

I woudlike to put some parameter in my request (code value and name value) :

    String code = "001";
    String name = "AAA";       

    HttpGet request = new HttpGet(url);
    String auth = user + ":" + mdp;

    byte[] encodedAuth = Base64.encodeBase64(
            auth.getBytes(StandardCharsets.ISO_8859_1));

    String authHeader = "Basic " + new String(encodedAuth);

    request.setHeader(HttpHeaders.AUTHORIZATION, authHeader);

    HttpClient client = HttpClientBuilder.create().build();
    HttpResponse response = client.execute(request);

    int statusCode = response.getStatusLine().getStatusCode();

How can I do this with authentification request ?

Something like : code=001&name=AAA in the URL

4
  • Add the parameters to url, e.g., url += "?" + code=001&name=AAA". This is somewhat ugly, so there are probably (hopefully) more elegant ways to build the string used in HttpGet. Those parameters should also be properly encoded. Commented Mar 8, 2021 at 13:11
  • @AndrewS How can I do that more elegant ? Commented Mar 8, 2021 at 13:15
  • 1
    This should help. Commented Mar 8, 2021 at 13:18
  • @AndrewS Thank you very much ! that's exactly what I'm looking for ! Commented Mar 8, 2021 at 13:19

1 Answer 1

0

Use a URIBuilder to construct your URL

import org.apache.http.client.utils.URIBuilder;
//...
final String URI_PARAM_CODE = "code";
final String URI_PARAM_NAME = "name";

String code = "001";
String name = "AAA";

URI uri = new URIBuilder("http://google.com")
    .setParameter(URI_PARAM_CODE, code)
    .setParameter(URI_PARAM_NAME, name)
    .build();

You can also set other useful attributes before calling URIBuilder::build - please refer to Apache URIBuilder documentation.

Sign up to request clarification or add additional context in comments.

Comments

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.