Java Jersey-client header issue
<p>i'm trying to add the following headers to a rest Post call...  it works in plain Java but i'm trying to re-write it using the Jersey client library...  When I make the post with Jersey I get an error code which isn't listed in the API documentation so i know it must just be a small issue like a missing header...  Any idea what i'm doing wrong in the bottom function?</p>

<p>Plain Java add headers function that works:</p>

<pre><code>private void SetDefaultHeaders(HttpURLConnection conn) {
        setRequestProperty(conn, "Accept", "*");
        setRequestProperty(conn, "Content-Type", "application/x-www-form-urlencoded");
}
</code></pre>

<p>Jersey code:</p>

<pre><code>public void logIn(String email, String password) {
        if (email != "" &amp;&amp; email != null &amp;&amp; password != "" &amp;&amp; password != null) {
            try {
                StringBuilder sb = new StringBuilder();
                sb.append(Settings.WIFIPLUG_URL);
                sb.append("/user_login");

MultivaluedMap&lt;String, String&gt; body = new MultivaluedMapImpl();
                body.add("username=", email);
                body.add("password=", password);    

                System.out.println("login url: " + sb.toString());

                WebResource webResource = Client.create(new DefaultClientConfig()).resource(sb.toString());

                WebResource.Builder builder = webResource.accept("*");
                builder.type("application/x-www-form-urlencoded");

                ClientResponse response = builder.post(ClientResponse.class, body);

                if (response.getStatus() != 200) {
                    throw new RuntimeException("failed: http error code " + response.getStatus());
                }
                System.out.println("Response from server: " + response.getEntity(String.class));

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
</code></pre>

<p>Full vanilla java login function:</p>

<pre><code>public String postUserLogin(String username, String password) {
        String result = "";
        // URL for API to login
        String url = "https://wifiplugapi.co.uk:3081/zcloud/api/user_login";
        String requestParams = "username=" + username + "&amp;password=" + password;

        try {
            URL obj = new URL(url);
            System.out.println("login url: " + obj);

            // Opens the connection
            HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

            // Send POST request
            con.setDoOutput(true);
            con.setDoInput(true);

            // Request Headers
            con.setRequestMethod("POST");

            // Sets all the headers
            SetDefaultHeaders(con);

            OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream(), "UTF-8");
            wr.write(requestParams);// adds values to the request
            wr.flush();
            wr.close();

            // Handles the response
            StringBuilder sb = new StringBuilder();
            int responseCode = con.getResponseCode();
            if (responseCode == 200) {
                // if the request was successful OK = 200
                BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
                String line = null;
                while ((line = br.readLine()) != null) {
                    sb.append(line + "\n");
                }
                br.close();
                // Returns Token
            } else {
                // If the request was bad, reason will be printed
                result = "Error, login request failed";
                throw new RuntimeException("Failed : HTTP error code : " + con.getResponseCode());
            }

            result = sb.toString();

            // JSON Parser
            JsonParser parser = new JsonParser();

            JsonObject resultObj = parser.parse(result).getAsJsonObject();
            con.disconnect();

            if (resultObj.get("token") != null) {
                result = (resultObj.get("token")).toString();
                System.out.println("JSONObject Result (token): " + result);
            } else {
                System.out.println("result = " + result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // returns token value in string ie. fdg573gb3789gv923378gy83g3
        result = result.replaceAll("\"", "");
        return result;
    }
</code></pre>