Simple URLConnection Failure

While trying to diagnose an Android project issue, I ran into the following problem:

Dim conn As New URLConnection
Dim s As String = conn.SendSync("GET", "https://www.apple.com/")

Dim headers() As String 

For Each header As pair In conn.ResponseHeaders
  headers.Add header.Left.stringvalue + ": " + header.Right.StringValue
Next

Break

This code appears to hang at the SendSync call, but it’s hard to tell because the debugger shows the breakpoint on the next line.

I’ve tried
• Different methods (GET, POST, PUT, HEAD)
• Adding Timeouts (5, 10, 30)

Does this code work for anyone else?

It is strange to me what is reported in the debugger for conn

I don’t even get that far

Use the Send() instead of the SendSync() method of the URLConnection.

Then read the response in the ContentReceived event of the URLConnection

well yeah, but I still don’t get any data back in any of the events.

I am able to get the response headers when sending asynchronously, via the HeadersReceived Event. However, I never get headers when sending synchronously.

Also, an observation: The response headers aren’t received / displayed in the order that they’re returned by the server. That shouldn’t be a problem, but it is good to know.

The content seems to come in normally when making requests to other sites, including xojo.com - and it does so whether sending asynchronously or synchronously requests.

When making a request to apple.com (either synchronously or synchronously), I can see the response content in the debugger. However, it’s only visible when choosing to see it as Binary data, or by changing the Text “View As” option to something like UTF-32.

And here’s something odd: If you look at the raw response from apple.com, you’ll see that after the response headers, there are ~40 empty lines before the “DOCTYPE” tag. I don’t know why Apple’s doing this, but I suspect that might be the cause of all this weirdness.

Interesting. On my machine, the HeadersReceived and ContentReceived events never fire when running on Android. Everything works as expected on the other platforms.

I wonder if it has to do with the Emulator?

I’m emulating a Pixel 3a API 30, w/ 1536 MB RAM, and 6144 MB Internal Storage.

I hope that’s not the case, or it’s going to be really confusing to users.

Mine is a Pixel 4 API 30, 1536 / 6144.

You’d think that a slightly newer one would work better, not worse.

So it turns out that the content is being received. It doesn’t look like anything’s coming in (especially in the editor) because of those 40 blank lines.

Not on mine. In sync mode it hangs and in async mode the ContentReceived event never fires.

It seems I have a similar problem.

Trying to send emails with the SendGrid API. Code works perfectly on iOS and Desktop.

Using SendSync
SendGrid does receive the API call and successfully sends the email.
HTTPstatus code is 202 meaning SendGrid received the request.

Using Send
SendGrid does receive the API call and successfully sends the email.
Error event fires.

Error.Message:

https://api.Sendgrid.com/v3/mail/send is not valid

This is worrying…

This is a hunch, but could it be that when sending asynchronously, Xojo is treating anything other than a “200” response as if it’s an error?

If so, then receiving something like a 202 (Accepted) or 204 (No Content) might cause the Error event to fire.

I suspect the same.

https://tracker.xojo.com/xojoinc/xojo/-/issues/73919

https://tracker.xojo.com/xojoinc/xojo/-/issues/73919 is now fixed :+1:

Just discovered something @Greg_O

My home Internet was down so I plugged my iPhone to my Mac to share the network connection. Whatever I did, the Android Emulator couldn’t process any request.

As soon as Internet (over wifi) was fixed, the Android Emulator could process HTTP requests. Could this be something similar to your experience?

I don’t recall if I was having that problem that day or not.