Connection reset while connecting with sharepoint online from java graph API

%3CLINGO-SUB%20id%3D%22lingo-sub-2380604%22%20slang%3D%22en-US%22%3EConnection%20reset%20while%20connecting%20with%20sharepoint%20online%20from%20java%20graph%20API%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2380604%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ewhen%20trying%20to%20connect%20to%20sharepoint%20using%20graph%20api%2C%20via%20HttpsUrlConnection%20as%20below%3A%3C%2FP%3E%3CP%3ETrustManager%5B%5D%20dummyTrustManager%20%3D%20new%20TrustManager%5B%5D%20%7B%20new%20X509TrustManager()%20%7B%3CBR%20%2F%3Epublic%20java.security.cert.X509Certificate%5B%5D%20getAcceptedIssuers()%20%7B%3CBR%20%2F%3Ereturn%20null%3B%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3COL%20class%3D%22linenums%22%3E%3CLI%3E%3CSPAN%20class%3D%22kwd%22%3Epublic%20%3CSPAN%20class%3D%22kwd%22%3Evoid%3CSPAN%20class%3D%22pln%22%3E%20checkClientTrusted%3CSPAN%20class%3D%22pun%22%3E(%3CSPAN%20class%3D%22pln%22%3EX509Certificate%3CSPAN%20class%3D%22pun%22%3E%5B%5D%3CSPAN%20class%3D%22pln%22%3E%20certs%3CSPAN%20class%3D%22pun%22%3E%2C%20%3CSPAN%20class%3D%22typ%22%3EString%3CSPAN%20class%3D%22pln%22%3E%20authType%3CSPAN%20class%3D%22pun%22%3E)%20%3CSPAN%20class%3D%22pun%22%3E%7B%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FLI%3E%3CLI%3E%3CSPAN%20class%3D%22pun%22%3E%7D%3C%2FSPAN%3E%3C%2FLI%3E%3CLI%3E%3CSPAN%20class%3D%22kwd%22%3Epublic%20%3CSPAN%20class%3D%22kwd%22%3Evoid%3CSPAN%20class%3D%22pln%22%3E%20checkServerTrusted%3CSPAN%20class%3D%22pun%22%3E(%3CSPAN%20class%3D%22pln%22%3EX509Certificate%3CSPAN%20class%3D%22pun%22%3E%5B%5D%3CSPAN%20class%3D%22pln%22%3E%20certs%3CSPAN%20class%3D%22pun%22%3E%2C%20%3CSPAN%20class%3D%22typ%22%3EString%3CSPAN%20class%3D%22pln%22%3E%20authType%3CSPAN%20class%3D%22pun%22%3E)%20%3CSPAN%20class%3D%22pun%22%3E%7B%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FLI%3E%3CLI%3E%3CSPAN%20class%3D%22pun%22%3E%7D%3C%2FSPAN%3E%3C%2FLI%3E%3CLI%3E%3CSPAN%20class%3D%22pun%22%3E%3CSPAN%20class%3D%22pun%22%3E%7D%20%3CSPAN%20class%3D%22pun%22%3E%7D%3B%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%26nbsp%3B%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESSLContext%20sc%20%3D%20SSLContext.getInstance(%22TLSv1.2%22)%3B%3CBR%20%2F%3Esc.init(null%2C%20dummyTrustManager%2C%20new%20java.security.SecureRandom())%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3COL%20class%3D%22linenums%22%3E%3CLI%3E%3CSPAN%20class%3D%22typ%22%3E%3CSPAN%20class%3D%22typ%22%3EString%3CSPAN%20class%3D%22pln%22%3E%20token%20%3CSPAN%20class%3D%22pun%22%3E%3D%3CSPAN%20class%3D%22pln%22%3E%20helper%3CSPAN%20class%3D%22pun%22%3E.%3CSPAN%20class%3D%22pln%22%3EgetToken%3CSPAN%20class%3D%22pun%22%3E()%3B%20%3CSPAN%20class%3D%22com%22%3E%2F%2Fget%20oauth2%20token%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3CPRE%3E%26nbsp%3B%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EURL%20url%20%3D%20new%20URL(newUrl)%3B%20%2F%2Finput%20url%3CBR%20%2F%3EHttpsURLConnection%20urlConnection%20%3D%20(HttpsURLConnection)%20url.openConnection()%3B%3CBR%20%2F%3EurlConnection.setSSLSocketFactory(sc.getSocketFactory())%3B%3CBR%20%2F%3EurlConnection.setRequestMethod(%22GET%22)%3B%3CBR%20%2F%3EurlConnection.setRequestProperty(%22Authorization%22%2C%20%22Bearer%20%22%20%2B%20token)%3B%3CBR%20%2F%3EurlConnection.setRequestProperty(%22Content-Type%22%2C%20%22application%2F%22%20%2B%20%22json%22)%3B%3C%2FP%3E%3CP%3EurlConnection.connect()%3B%3C%2FP%3E%3CP%3Eit%20works%20in%20windows%2C%20but%20fails%20in%20linux%20machine%20with%20below%20error%3A%3C%2FP%3E%3CP%3Ejava.net.SocketException%3A%20Connection%20reset%3CBR%20%2F%3Eat%20java.net.SocketInputStream.read(SocketInputStream.java%3A196)%3CBR%20%2F%3Eat%20java.net.SocketInputStream.read(SocketInputStream.java%3A122)%3CBR%20%2F%3Eat%20sun.security.ssl.InputRecord.readFully(InputRecord.java%3A442)%3CBR%20%2F%3Eat%20sun.security.ssl.InputRecord.read(InputRecord.java%3A480)%3CBR%20%2F%3Eat%20sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java%3A927)%3CBR%20%2F%3Eat%20sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java%3A1312)%3CBR%20%2F%3Eat%20sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java%3A1339)%3CBR%20%2F%3Eat%20sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java%3A1323)%3CBR%20%2F%3E..%3C%2FP%3E%3CP%3Ejava%20version%20in%20linux%20is%20%3A%3CBR%20%2F%3Eroot%40workflow-test-new%3A%2Fhome%2Ftomcat7%2Fapache-tomcat-7%2Flogs%23%20java%20-version%3CBR%20%2F%3Ejava%20version%20%221.7.0_45%22%3CBR%20%2F%3EJava(TM)%20SE%20Runtime%20Environment%20(build%201.7.0_45-b18)%3CBR%20%2F%3EJava%20HotSpot(TM)%2064-Bit%20Server%20VM%20(build%2024.45-b08%2C%20mixed%20mode)%3C%2FP%3E%3C%2FLI%3E%3C%2FOL%3E%3C%2FLI%3E%3C%2FOL%3E%3C%2FLINGO-BODY%3E
New Contributor

 

when trying to connect to sharepoint using graph api, via HttpsUrlConnection as below:

TrustManager[] dummyTrustManager = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}

 

  1. public void checkClientTrusted(X509Certificate[] certs, String authType) {
  2. }
  3. public void checkServerTrusted(X509Certificate[] certs, String authType) {
  4. }
  5. } };

     

     

     

    SSLContext sc = SSLContext.getInstance("TLSv1.2");
    sc.init(null, dummyTrustManager, new java.security.SecureRandom());

     

    1. String token = helper.getToken(); //get oauth2 token
       

       

      URL url = new URL(newUrl); //input url
      HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
      urlConnection.setSSLSocketFactory(sc.getSocketFactory());
      urlConnection.setRequestMethod("GET");
      urlConnection.setRequestProperty("Authorization", "Bearer " + token);
      urlConnection.setRequestProperty("Content-Type", "application/" + "json");

      urlConnection.connect();

      it works in windows, but fails in linux machine with below error:

      java.net.SocketException: Connection reset
      at java.net.SocketInputStream.read(SocketInputStream.java:196)
      at java.net.SocketInputStream.read(SocketInputStream.java:122)
      at sun.security.ssl.InputRecord.readFully(InputRecord.java:442)
      at sun.security.ssl.InputRecord.read(InputRecord.java:480)
      at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:927)
      at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312)
      at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339)
      at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323)
      ..

      java version in linux is :
      root@workflow-test-new:/home/tomcat7/apache-tomcat-7/logs# java -version
      java version "1.7.0_45"
      Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
      Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)

0 Replies