We help IT Professionals succeed at work.

HttpOpenRequest WinINet - invalid URl with port 993? (C++)

Hi,

I'm trying to use HttpOpenRequest to post to a web service on port 993 (code below). Unfortunately I always get error code 12005 - invalid URL. Why is that? On other ports like 80 or 443 I get other errors like 'can not connect'. I know 993 is the imaps port but it's not blocked - I see that in netstat. Why can't I open a request to this port?

Thanks!


-------------------------------------
#include <windows.h>
#include <wininet.h>
#include <stdio.h>

#pragma comment(lib, "wininet.lib")

int main(int argc, char *argv[])
{
      HINTERNET hInternet = NULL;
      HINTERNET hConnect = NULL;
      HINTERNET hRequest = NULL;
      DWORD size  = 0;
      BOOL bRetVal = TRUE;
      
      hInternet = InternetOpen(TEXT("slim-jim-proxy"),
      INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);

      if (hInternet == NULL)
      {
            printf("hInternet == NULL\n");
            //return FALSE;
      }

      hConnect = InternetConnect(hInternet, TEXT("localhost"), 993, NULL,
      NULL, INTERNET_SERVICE_HTTP, 0, 0);

      if (hConnect == NULL)
      {
        printf("hConnect == NULL\n");
        InternetCloseHandle(hInternet);
        //return FALSE;
      }

    DWORD errorCode = 0;


      hRequest = HttpOpenRequest(hConnect, TEXT("POST"),
      TEXT(""), TEXT("HTTP/1.1"), NULL, NULL,
      INTERNET_FLAG_NO_CACHE_WRITE |
      INTERNET_FLAG_NO_COOKIES |
      INTERNET_FLAG_NO_AUTH |
      INTERNET_FLAG_SECURE
      | INTERNET_FLAG_IGNORE_CERT_CN_INVALID
      | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID, NULL);
      //TEXT("gmail"), TEXT("HTTP/1.1"), NULL, NULL,
      //INTERNET_FLAG_RELOAD, NULL);

      errorCode = GetLastError();
      printf("errorCode: %d\n", errorCode);
      if (hRequest == NULL)
      {
            printf("hRequest == NULL\n");
            InternetCloseHandle(hConnect);
            InternetCloseHandle(hInternet);

            //return FALSE;
      }


      


      InternetCloseHandle(hRequest);
      InternetCloseHandle(hConnect);
      InternetCloseHandle(hInternet);

      system("pause");

}
Comment
Watch Question

Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
Is there a service for that port running on the server?  Regular IMAP - port 143, IMAP4 over SSL (IMAPS) - port 993.  If this is an Exchange Server, you might 'Request Attention' and get that zone included in your question.

Author

Commented:
Dave, thanks for your mail. It's strange, using Wireshark I don't even see the client trying to access the IP or host I specify in the C++ program when I use port 993. When I use other ports, I see the request in Wireshark...
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
Oh, that's your C++ runtime (WinInet?) telling you that it doesn't understand your URL so it doesn't connect.  http://support.microsoft.com/kb/193625  You're probably missing something in  hConnect = InternetConnect(hInternet, TEXT("localhost"), 993, NULL,  NULL, INTERNET_SERVICE_HTTP, 0, 0);.
Are you sure that response is returned because of the failure of that particular function? I see that you fetch the error code before checking for the NULL return value. As for wireshark, it has issues with sniffing the localhost traffic. There are ways to sniff the loopback traffic but those are not reliable. Are you able to see traffic for the other localhost ports?
Lastly, can you also try by specifying an object name in the openrequest function?
Just for curiosity: why dont you use WinHttp rather than WinInet? its better in at least it has tracing features that let you see whats going on inside.
jkr
Top Expert 2012

Commented:
>>I always get error code 12005 - invalid URL. Why is that?

You are not providing any URL - thus the error. In

      hRequest = HttpOpenRequest(hConnect, TEXT("POST"),
      TEXT(""), TEXT("HTTP/1.1"), NULL, NULL,
      INTERNET_FLAG_NO_CACHE_WRITE |
      INTERNET_FLAG_NO_COOKIES |
      INTERNET_FLAG_NO_AUTH |
      INTERNET_FLAG_SECURE
      | INTERNET_FLAG_IGNORE_CERT_CN_INVALID
      | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID, NULL);

you are just passing 'TEXT("")', which simply is not a valid URL. Try something like 'TEXT("index.htm")' at least. See also http://msdn.microsoft.com/en-us/library/aa384233%28VS.85%29.aspx ("HttpOpenRequest Function"):

pszObjectName [in]

    A pointer to a null-terminated string that contains the name of the target object of the specified HTTP verb. This is generally a file name, an executable module, or a search specifier.

Author

Commented:
ambience, thanks for your post. I'm sure the HttpOpenRequest fails and returns 12005. I checked Wireshark as well when I used a different remote IP with the same results. I didn't see any traffic when port 993 was used. I tried with WinHttp and it seems to work with that port - I might go this route if I can't resolve the issue.

jkr - thanks. I tried with and without providing a URL. I get the same result. For 993 port I get invalid URL, other ports have different error messages. It's weird...
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
Check the docs for that function.  I'm not sure it supports what you want to do.

Author

Commented:
offered a workaround solution