1

I have to make a POST request to an ASP WEB APi 2 with OWIN login which is expecting the following body:

grant_type=password
password=qwerty
username=administrator

However, I always get FileNotFound Exception with a 400 responseCode. I'm not really sure if I'm sending the POST data the correct way. Here is my code:

public JSONObject getLoginToken(String username, String password) {
        URL url = null;
        HttpURLConnection httpURLConnection = null;
        BufferedReader bufferedReader = null;
        JSONObject response = null;

        try {
            url = new URL("someUrl");
            httpURLConnection = (HttpURLConnection) url.openConnection();

            JSONObject data = new JSONObject();
            data.put("username", username);
            data.put("password", password);
            data.put("grant_type", "password");

            httpURLConnection.setChunkedStreamingMode(0);
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);

            OutputStream os = httpURLConnection.getOutputStream();
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));
            writer.write(URLEncoder.encode(data.toString(), "UTF-8"));
            writer.flush();
            writer.close();
            os.close();

            httpURLConnection.connect();

            bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream())); // <-- fails here

            StringBuilder sb = new StringBuilder();
            String line = null;

            while ((line = bufferedReader.readLine()) != null) {
                sb.append(line + "\n");
            }

            response = new JSONObject(sb.toString());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (httpURLConnection != null) {
                httpURLConnection.disconnect();
            }
        }

        return response;
    }

EDIT

Here is how the endpoint is created in C#, if it's of any help.

var client = new RestClient(Statics.API_LOCATION_URL + "login");
var request = new RestRequest(Method.POST);
request.AddParameter("grant_type", "password");
request.AddParameter("username", "administrator");
request.AddParameter("password", "qwerty");
XeniaSis
  • 2,192
  • 5
  • 24
  • 39

1 Answers1

0

no, it's not proper way. HERE you can find two useful answers how to do that properly (I'm recomending second one, not marked)

Community
  • 1
  • 1
snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • tried that already like this `String data = URLEncoder.encode("username=" + username + "&password=" + password + "&grant_type=password", "UTF-8");`. Still got same error – XeniaSis Jun 21 '16 at 20:05
  • nothing more I don't see... I would try to look for answer on server-side... also you wrote "...expecting the following **body**..." - you sure you have to send params in POST method? also you are using `setChunkedStreamingMode(0)` and from doc: "...Note, **not all** HTTP servers support this mode.". also: set some timeouts ;) all I can say about, good luck! – snachmsm Jun 21 '16 at 20:10
  • checking that server-side isn't "filtering" by headers or any other param I can't suggest anything... your code looks so simple and "perfect" ;) maybe you can check more about 400 error, [like here](http://stackoverflow.com/questions/19671317/400-bad-request-http-error-code-meaning) – snachmsm Jun 21 '16 at 20:40