Convert Jersey Client to HttpUrlConnection

adbdkb :

I have a Jersey client that retrieves a jwt token from an API.

This is the code

public static final String INFO_ENDPOINT = "http://10.1.9.10:7100/Info/";
private static final String INFO_USERNAME = "user";
private static final String INFO_PASSWORD = "password";
private static final String AUTH_PATH = "auth";
private String token;
private final Client client;

public JerseyClient() {
    ClientConfig cc = new DefaultClientConfig();

    cc.getClasses().add(MultiPartWriter.class);
    client = Client.create(cc);
}

public void authenticate() {
    try {
        WebResource resource = client.resource(INFO_ENDPOINT + AUTH_PATH);
        StringBuilder sb = new StringBuilder();
        sb.append("{\"username\":\"" + INFO_USERNAME + "\",");
        sb.append("\"password\":\"" + INFO_PASSWORD + "\"}");
        ClientResponse clientResp = resource.type("application/json")
                                            .post(ClientResponse.class, sb.toString());
        String content = clientResp.getEntity(String.class);
        System.out.println("Response:" + content);
        token = content.substring(content.indexOf("\"token\":") + 9,
                                  content.lastIndexOf("\""));
        System.out.println("token " + token);
    } catch (ClientHandlerException | UniformInterfaceException e) {
    }
}

The above code returns a jwt token that is then used as a key for another call.

I am trying to convert it to use HttpUrlConnection. But that does not seem to work

This is what I have tried. It does not give an error, but does not return the token either. The response is empty

try {
    URL url = new URL("http://10.1.9.10:7100/Info/auth");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setDoOutput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/json");
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setRequestProperty("accept", "application/json");

    String input = "{\"username\":user,\"password\":\"password\"}";

    OutputStream os = conn.getOutputStream();
    os.write(input.getBytes());
    os.flush();

    if (conn.getResponseCode() != 200) {
        throw new RuntimeException("Failed : HTTP error code : "
           + conn.getResponseCode());
    }

    BufferedReader br = new BufferedReader(new InputStreamReader(
       (conn.getInputStream())));

    String output;
    System.out.println("Output from Server .... \n");
    while ((output = br.readLine()) != null) {
        System.out.println(output);
    }

    conn.disconnect();

} catch (IOException e) {
}

My problem is I am not able to translate .post(ClientResponse.class, sb.toString()) to HttpUrlConnection.

What am I missing, or what am I doing wrong?

Thanks

adbdkb :

The issue is resolved. The code works as is.

The problem was I was missing quotes for json value String

Should have been

String input = "{\"username\":\"user\",\"password\":\"password\"}";

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=162618&siteId=1