B4J Question httpjob just hangs (server has SSL Verification error)

alwaysbusy

Expert
Licensed User
Longtime User
For a client, I need to access a certain REST Api they wrote. They do have a SSL verification error. In Postman, I can disable this verification and then it works.

This is the code:
B4X:
public Sub RunRestAPI()    
    Dim j As HttpJob
    j.Initialize("", Me)
    
    Dim jsonB As String = $"{
    "otId": "${authCurrentMasterID}",
    "task": "push_registrations",
    "parameters": {
      "date_from": "2021-10-14 00:00:00",
      "date_until": "2021-10-15 23:59:59"
    }
}"$

    j.PostString($"https://sync.onetwo.klanten.webdoos.io/bouwsoft/task/create"$, jsonB)
    j.GetRequest.SetContentType("application/json")
    Wait For (j) Jobdone(j As HttpJob)
    If j.Success Then
        Log("Success")        
    Else
        Log(j.ErrorMessage)        
    End If
    j.Release    
End Sub

I have tried to add the HU2_ACCEPTALL in Build Configurations, but with it or not, the job just seems to 'hang'. I never get past the Wait For so I never see one of the two logs.

In PostMan with the 'Disable Versification' OFF:

1635169694383.png


In PostMan with the 'Disable Versification' ON:

1635169745517.png


Any ideas?

Alwaysbusy
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub AppStart (Args() As String)
    Log("Hello world!!!")
    RunRestAPI
    StartMessageLoop
End Sub

public Sub RunRestAPI()
    Dim j As HttpJob
    j.Initialize("", Me)
    Dim authCurrentMasterID As String = "aaa"
    Dim jsonB As String = $"{
    "otId": "${authCurrentMasterID}",
    "task": "push_registrations",
    "parameters": {
      "date_from": "2021-10-14 00:00:00",
      "date_until": "2021-10-15 23:59:59"
    }
}"$

    j.PostString($"https://sync.onetwo.klanten.webdoos.io/bouwsoft/task/create"$, jsonB)
    j.GetRequest.SetContentType("application/json")
    Wait For (j) Jobdone(j As HttpJob)
    If j.Success Then
        Log("Success")
    Else
'        Log(j.ErrorMessage)
    End If
    j.Release
End Sub

Output:
Waiting for debugger to connect...
Program started.
Hello world!!!
ResponseError. Reason: Internal Server Error, Response: <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="robots" content="noindex,nofollow,noarchive" />
<title>An Error Occurred: Internal Server Error</title>
<style>body { background-color: #fff; color: #222; font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; margin: 0; }
.container { margin: 30px; max-width: 600px; }
h1 { color: #dc3545; font-size: 24px; }
h2 { font-size: 18px; }</style>
</head>
<body>
<div class="container">
<h1>Oops! An Error Occurred</h1>
<h2>The server returned a "500 Internal Server Error".</h2>
<p>
Something is broken. Please let us know what you were doing when this error occurred.
We will fix it as soon as possible. Sorry for any inconvenience caused.
</p>
</div>
</body>
</html>


I haven't added HU2_ACCEPTALL. I don't see a SSL error.
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
Hmm, I don't even get that. It never reaches the 'If j.Success then' line. I also tried not using the Waitfor and doing it the old way, but the JobDone() method is not reached either.

What Java version are you using? I'm on OpenJdk 11 on our server. I hope an upgrade is not needed as our Network Engeneer will go nuts ;)

What do you get back if you use authCurrentMasterID = "4"? (that one is a valid code, and should return the success json.

EDIT: I should metion that I make this call from within a jServer project.

Alwaysbusy
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
EDIT: I should metion that I make this call from within a jServer project.
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
Thanks for the Resumable Sub suggestion. I tried it before (the code was called from withing a Websocket), but I will now try it with a normal Handler class.

Sorry for re-asking, but you are also on Java 11? I ask as I just see they use TLS 1.3 and OkHttp was reported to have problems with openJdk 11.
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
i can confirm here too that it works an reaches the success. b4j 9.10, jdk 11.

my suggestion is to check your firewalls, either of your pc or the routers, also your antivirus, mine sometimes kills the comm when detects something unusual
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
Probably a threading issue.
That is what I suspect too. I have narrowed it down that as soon as I use my ABMaterial library, OKHttpUtils stops working. As this project is rather urgent I just wrote a small method in Java that does the call without any issues and will go deeper into my ABMaterial code to try to find out who is causing this.

I was told the initial error I had seemed to be unrelated and had something to do with https://borncity.com/win/2021/09/30...e-trouble-with-old-lets-encrypt-certificates/. Not sure if this was the case here for me but after updating PostMan to the latest version, at least PostMan did work again making the call.

B4X:
Hello world!!!
javax.net.ssl.SSLPeerUnverifiedException: Hostname sync.onetwo.klanten.webdoos.io not verified (no certificates)
at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:337)
at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:284)
at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:169)
at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:258)
at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)
at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:127)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:257)
at okhttp3.RealCall.execute(RealCall.java:93)
at anywheresoftware.b4h.okhttp.OkHttpClientWrapper.executeWithTimeout(OkHttpClientWrapper.java:173)
at anywheresoftware.b4h.okhttp.OkHttpClientWrapper.access$0(OkHttpClientWrapper.java:170)
at anywheresoftware.b4h.okhttp.OkHttpClientWrapper$ExecuteHelper.run(OkHttpClientWrapper.java:218)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
ResponseError. Reason: javax.net.ssl.SSLPeerUnverifiedException: Hostname sync.onetwo.klanten.webdoos.io not verified (no certificates), Response:
javax.net.ssl.SSLPeerUnverifiedException: Hostname sync.onetwo.klanten.webdoos.io not verified (no certificates)

Temporary replacement code I use to make the call without any problems:
B4X:
public String MakeRESTApiCall(String method, String restUrl, String jsonBody, anywheresoftware.b4a.objects.collections.Map headers) {
            HttpsURLConnection urlConnection = null;
            try {
              URL url = new URL(restUrl);
              urlConnection =  (HttpsURLConnection) url.openConnection();

              urlConnection.setConnectTimeout(20000);
              urlConnection.setReadTimeout(20000);
              urlConnection.setDoOutput(true);
              urlConnection.setRequestMethod(method);
              
              final anywheresoftware.b4a.BA.IterableList keys = headers.Keys();
              final int keyLen = keys.getSize();        
              for (int i = 0;i < keyLen ;i++){
                    String key = (String) keys.Get(i);
                    String value = (String) headers.Get(key);
                    urlConnection.setRequestProperty(key, value);
              }
              urlConnection.connect();

              byte[] outputBytes = jsonBody.getBytes("UTF-8");
              OutputStream out = urlConnection.getOutputStream();
              out.write(outputBytes);
              out.flush();
              out.close();

              int statusCode = urlConnection.getResponseCode();
              String tmpCode="";
              if (statusCode != HttpURLConnection.HTTP_OK) {
                BA.Log("MakeRESTApiCall: connection failed: statusCode: " + statusCode);
                tmpCode = "connection failed -> statusCode: " + statusCode + "\n";
              }

              InputStream in = new BufferedInputStream(urlConnection.getInputStream());
              String responseText = convertInputStreamToString(in);
              return tmpCode + responseText;

            } catch (IOException e) {
              e.printStackTrace();
            } finally {
              if (urlConnection != null) {
                urlConnection.disconnect();
              }
            }
            return "";
    }

Alwaysbusy
 
Upvote 0
Top