How to handle WinHttpRequest Error Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
kashmirLZ
Posts: 56
Joined: 06 Oct 2022, 23:27

How to handle WinHttpRequest Error

20 Feb 2023, 07:10

We have a lovely function created by user Linear Spoon and ported from microsoft for checking internet connectivity here.

Code: Select all

IsInternetConnected()
{
  static sz := A_IsUnicode ? 408 : 204, addrToStr := "Ws2_32\WSAAddressToString" (A_IsUnicode ? "W" : "A")
  VarSetCapacity(wsaData, 408)
  if DllCall("Ws2_32\WSAStartup", "UShort", 0x0202, "Ptr", &wsaData)
    return false
  if DllCall("Ws2_32\GetAddrInfoW", "wstr", "dns.msftncsi.com", "wstr", "http", "ptr", 0, "ptr*", results)
  {
    DllCall("Ws2_32\WSACleanup")
    return false
  }
  ai_family := NumGet(results+4, 0, "int")    ;address family (ipv4 or ipv6)
  ai_addr := Numget(results+16, 2*A_PtrSize, "ptr")   ;binary ip address
  ai_addrlen := Numget(results+16, 0, "ptr")   ;length of ip
  DllCall(addrToStr, "ptr", ai_addr, "uint", ai_addrlen, "ptr", 0, "str", wsaData, "uint*", 204)
  DllCall("Ws2_32\FreeAddrInfoW", "ptr", results)
  DllCall("Ws2_32\WSACleanup")
  http := ComObjCreate("WinHttp.WinHttpRequest.5.1")

  if (ai_family = 2 && wsaData = "131.107.255.255:80")
  {
    http.Open("GET", "http://www.msftncsi.com/ncsi.txt")
  }
  else if (ai_family = 23 && wsaData = "[fd3e:4f5a:5b81::1]:80")
  {
    http.Open("GET", "http://ipv6.msftncsi.com/ncsi.txt")
  }
  else
  {
    return false
  }
  http.Send()
  return (http.ResponseText = "Microsoft NCSI") ;ncsi.txt will contain exactly this text
}
thread source


This code works well if the internet is connected.
This work works well if the internet is disconnected.
However if the internet connection is very spotty/laggy then the script crashes with this error.
error.png
error.png (11.57 KiB) Viewed 490 times
I would prefer to have the function return false if this function fails, as this poor internet connectivity should not be considered a valid stable connection.

How to catch and handle this error in AHK?
kashmirLZ
Posts: 56
Joined: 06 Oct 2022, 23:27

Re: How to handle WinHttpRequest Error  Topic is solved

21 Feb 2023, 01:28

Dear future readers, code updated:

Code: Select all

IsInternetConnected()
{
  static sz := A_IsUnicode ? 408 : 204, addrToStr := "Ws2_32\WSAAddressToString" (A_IsUnicode ? "W" : "A")
  VarSetCapacity(wsaData, 408)
  if DllCall("Ws2_32\WSAStartup", "UShort", 0x0202, "Ptr", &wsaData)
    return false
  if DllCall("Ws2_32\GetAddrInfoW", "wstr", "dns.msftncsi.com", "wstr", "http", "ptr", 0, "ptr*", results)
  {
    DllCall("Ws2_32\WSACleanup")
    return false
  }
  ai_family := NumGet(results+4, 0, "int")    ;address family (ipv4 or ipv6)
  ai_addr := Numget(results+16, 2*A_PtrSize, "ptr")   ;binary ip address
  ai_addrlen := Numget(results+16, 0, "ptr")   ;length of ip
  DllCall(addrToStr, "ptr", ai_addr, "uint", ai_addrlen, "ptr", 0, "str", wsaData, "uint*", 204)
  DllCall("Ws2_32\FreeAddrInfoW", "ptr", results)
  DllCall("Ws2_32\WSACleanup")
  http := ComObjCreate("WinHttp.WinHttpRequest.5.1")
  
  try {
    if (ai_family = 2 && wsaData = "131.107.255.255:80")
    {
      http.Open("GET", "http://www.msftncsi.com/ncsi.txt")
    }
    else if (ai_family = 23 && wsaData = "[fd3e:4f5a:5b81::1]:80")
    {
      http.Open("GET", "http://ipv6.msftncsi.com/ncsi.txt")
    }
    else
    {
      return false
    }
    http.Send()
    return (http.ResponseText = "Microsoft NCSI") ;ncsi.txt will contain exactly this text
  } catch e {
    return false
  }
}
kashmirLZ
Posts: 56
Joined: 06 Oct 2022, 23:27

Re: How to handle WinHttpRequest Error

21 Feb 2023, 01:29

malcev wrote:
20 Feb 2023, 13:39
viewtopic.php?f=6&t=113844
Thanks for sharing. Code looks good, but I've no idea how to use it or integrate it into my script as easily as I can with the other self-contained function.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Draken, oktavimark, Spawnova, william_ahk and 284 guests