HTTP GET请求在java中给出500错误

时间:2014-11-25 09:32:41

标签: java http office365 postman

我正试图点击Office 365的'getMessages'api。我尝试在java中跟踪url和authorization头。但是它给出了RESPONSE代码500,这是一个内部服务器错误。但是当我在POSTMAN REST CLIENT中尝试相同的操作时,它完全正常。任何人都可以建议我做错了吗?

URL url = new URL("https://outlook.office365.com/api/v1.0/me/messages/"); 
        String asB64 = "Basic "+new String(new Base64().encode(user+":"+password.getBytes()));
        println "asB64 : " + asB64;
        HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
        urlConnection.setRequestProperty ("Authorization", asB64);
        urlConnection.setDoOutput(true);
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoInput(true);
        urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded,charset=UTF-8");

        PrintWriter printWriter = new PrintWriter(urlConnection.getOutputStream());

        printWriter.flush();

        InputStream inputStream = urlConnection.getInputStream();
        String contentType = urlConnection.getContentType();
        println "output : " + urlConnection.getResponseCode() + " message : " + urlConnection.getResponseMessage() + " error : " + urlConnection.getErrorStream();

以下应该是请求的输出(POSTMAN REST CLIENT按原样提供以下响应):

{"@odata.context":"https://outlook.office365.com/api/v1.0/$metadata#Me/Messages","value":[{"@odata.id":"https://outlook.office365.com/api/v1.0/Users('test2@lynx.be')/Messages('AAMkADdkMTEyY2RhLTA5YTItNDM0OS05NTVjLWZhNGI2OWUxZjBkOABGAAAAAACFIzMHuQuLQ6LXFS-gt6NSBwBgChXEzbNhRbE9LyPei8StAAAAAAEMAABgChXEzbNhRbE9LyPei8StAAACWUu4AAA=')","@odata.etag":"W/\"CQAAABYAAABgChXEzbNhRbE9LyPei8StAAACWUs2\"","Id":"AAMkADdkMTEyY2RhLTA5YTItNDM0OS05NTVjLWZhNGI2OWUxZjBkOABGAAAAAACFIzMHuQuLQ6LXFS-gt6NSBwBgChXEzbNhRbE9LyPei8StAAAAAAEMAABgChXEzbNhRbE9LyPei8StAAACWUu4AAA=","ChangeKey":"CQAAABYAAABgChXEzbNhRbE9LyPei8StAAACWUs2","Categories":[],"DateTimeCreated":"2014-11-25T06:34:35Z","DateTimeLastModified":"2014-11-25T06:34:35Z","Subject":"test me","BodyPreview":"Hello..\r\n\r\n- Piyusha","Body":{"ContentType":"HTML","Content":"\r\n\r\n\r\n\r\n\r\n\r\n\r\n
\r\n
Hello..

\r\n


\r\n
\r\n-         \r\nPiyusha

\r\n


\r\n
\r\n\r\n\r\n"},"Importance":"Normal","HasAttachments":false,"ParentFolderId":"AAMkADdkMTEyY2RhLTA5YTItNDM0OS05NTVjLWZhNGI2OWUxZjBkOAAuAAAAAACFIzMHuQuLQ6LXFS-gt6NSAQBgChXEzbNhRbE9LyPei8StAAAAAAEMAAA=","From":{"EmailAddress":{"Address":"pjadhav@prosperasoft.com","Name":"Piyusha"}},"Sender":{"EmailAddress":{"Address":"pjadhav@prosperasoft.com","Name":"Piyusha"}},"ToRecipients":[{"EmailAddress":{"Address":"test2@lynx.be","Name":"Test2"}}],"CcRecipients":[],"BccRecipients":[],"ReplyTo":[],"ConversationId":"AAQkADdkMTEyY2RhLTA5YTItNDM0OS05NTVjLWZhNGI2OWUxZjBkOAAQADdgtnhCYExDhAoxDCJGdbk=","DateTimeReceived":"2014-11-25T06:34:35Z","DateTimeSent":"2014-11-25T06:33:57Z","IsDeliveryReceiptRequested":null,"IsReadReceiptRequested":false,"IsDraft":false,"IsRead":false},{"@odata.id":"https://outlook.office365.com/api/v1.0/Users('test2@lynx.be')/Messages('AAMkADdkMTEyY2RhLTA5YTItNDM0OS05NTVjLWZhNGI2OWUxZjBkOABGAAAAAACFIzMHuQuLQ6LXFS-gt6NSBwBgChXEzbNhRbE9LyPei8StAAAAAAEMAABgChXEzbNhRbE9LyPei8StAAACWUu3AAA=')","@odata.etag":"W/\"CQAAABYAAABgChXEzbNhRbE9LyPei8StAAACWUs1\"","Id":"AAMkADdkMTEyY2RhLTA5YTItNDM0OS05NTVjLWZhNGI2OWUxZjBkOABGAAAAAACFIzMHuQuLQ6LXFS-gt6NSBwBgChXEzbNhRbE9LyPei8StAAAAAAEMAABgChXEzbNhRbE9LyPei8StAAACWUu3AAA=","ChangeKey":"CQAAABYAAABgChXEzbNhRbE9LyPei8StAAACWUs1","Categories":[],"DateTimeCreated":"2014-11-24T05:06:24Z","DateTimeLastModified":"2014-11-24T05:06:24Z","Subject":"Test Email for API","BodyPreview":"Hi,\r\nThis is test email for API testing\r\n\r\n- Piyusha","Body":{"ContentType":"HTML","Content":"\r\n\r\n\r\n\r\n\r\n\r\n\r\n
\r\n


\r\n
Hi,

\r\n
This is test email for API testing

\r\n


\r\n
\r\n-         \r\nPiyusha

\r\n


\r\n
\r\n\r\n\r\n"},"Importance":"Normal","HasAttachments":false,"ParentFolderId":"AAMkADdkMTEyY2RhLTA5YTItNDM0OS05NTVjLWZhNGI2OWUxZjBkOAAuAAAAAACFIzMHuQuLQ6LXFS-gt6NSAQBgChXEzbNhRbE9LyPei8StAAAAAAEMAAA=","From":{"EmailAddress":{"Address":"pjadhav@prosperasoft.com","Name":"Piyusha"}},"Sender":{"EmailAddress":{"Address":"pjadhav@prosperasoft.com","Name":"Piyusha"}},"ToRecipients":[{"EmailAddress":{"Address":"test2@lynx.be","Name":"Test2"}}],"CcRecipients":[],"BccRecipients":[],"ReplyTo":[],"ConversationId":"AAQkADdkMTEyY2RhLTA5YTItNDM0OS05NTVjLWZhNGI2OWUxZjBkOAAQAJE40Qula0HBl35-_7JOrmM=","DateTimeReceived":"2014-11-24T05:06:24Z","DateTimeSent":"2014-11-24T05:05:55Z","IsDeliveryReceiptRequested":null,"IsReadReceiptRequested":false,"IsDraft":false,"IsRead":false}]}

1 个答案:

答案 0 :(得分:0)

我使用Unirest for Java解决了这个问题。

我使用了以下代码:

HttpResponse<JsonNode> response = Unirest.get("https://outlook.office365.com/api/v1.0/me/messages").basicAuth(user, password).asJson();

println "response : " + response.getBody();

上面的一个班轮代码对我来说很少有依赖性变化。 :)