【发布时间】:2016-03-05 22:13:19
【问题描述】:
我想将简单的 JSON 发布到 Web 服务并接收响应,但我在 client.getResponseCode() 上收到 java.io.IOException : No authentication challenges found 错误
我尝试了this solution,但它对我不起作用。
这是我的代码:
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();
}
【问题讨论】:
标签: android json post httpurlconnection