我可以使用microsoft outlook api进行休息调用。这是我写的代码。
public static void sendGet() {
String url = "https://outlook.office365.com/api/v1.0/me/folders/Inbox/messages";
final String CONTENT_TYPE = "application/json";
final String ACCEPT_LANGUAGE = "en-US,en;q=0.8";
try {
URL obj = new URL(url);
HttpsURLConnection connection = (HttpsURLConnection) obj.openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Content-Type", CONTENT_TYPE);
connection.setRequestProperty("Accept-Language", ACCEPT_LANGUAGE);
connection.setRequestProperty("Authorization", "Basic c2h1YW5nQHZpdfdGVjaGluYydf5jb2fdXjhNCE="); //base64 encoding of auth username:password
int responseCode = connection.getResponseCode();
System.out.println("response code: " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我正在尝试返回包含收件箱中所有邮件的json响应,但它返回了一个错误代码为406的IO异常。
我很确定支持内容类型“application / json”,当我使用post man执行其余调用时,它将能够成功地返回json数据。
从邮递员标题中,支持application / json的内容类型。谁知道我做错了什么?
答案 0 :(得分:2)
答案 1 :(得分:1)
您是否尝试将Accept: application/json
添加到HTTP标头。
Accept
标头告诉服务器它们接受哪些内容类型。然后,服务器发回一个响应,其中包含一个Content-Type
标头,告诉客户端返回内容的内容类型实际上是什么。
tldr; Accept
是客户端能够使用的内容,Content-Type
是数据的实际内容。