I want to post simple JSON to webservice and receive response but I am getting java.io.IOException : No authentication challenges found
error on client.getResponseCode()
I tried this solution but it didn't work for me.
Here is my code:
StringBuilder sb = new StringBuilder();
HttpURLConnection client = null;
try {
URL url = new URL("http://myurl:3000");
client = (HttpURLConnection) url.openConnection();
client.setDoOutput(true);
client.setDoInput(true);
client.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
client.setRequestProperty("Authorization", "Basic " + Base64.encodeToString("userid:pwd".getBytes(), Base64.NO_WRAP));
client.setRequestMethod("POST");
client.connect();
OutputStreamWriter writer = new OutputStreamWriter(client.getOutputStream());
String output = json.toString();
writer.write(output);
writer.flush();
writer.close();
int HttpResult = client.getResponseCode();
if (HttpResult == HttpURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(new InputStreamReader(
client.getInputStream(), "utf-8"));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
}
} catch (IOException e) {
this.e = e;
} finally {
client.disconnect();
}