Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, everyone.
I have to check if given url is exist.

So I tried to use InternetCheckConnection() function.
But the program is hanging on that line.

What can be reason?
or do you have any other suggestion to check url is exist?
Posted
Comments
Sandeep Mewara 24-Jun-12 16:16pm    
WinINet does not support server implementations. In addition, it should not be used from a service
Hope you are following it. Right?
[no name] 24-Jun-12 16:20pm    
Thanks for your replying.
sorry, but what do you mean server implementations?
I think it's a windows api.

I injected dll to other exe and I have used that function in that dll.
Sandeep Mewara 25-Jun-12 1:45am    
I was talking as per mentioned here: http://msdn.microsoft.com/en-us/library/windows/desktop/aa384346%28v=vs.85%29.aspx

There is no such thing as verifying URL for existence. You can only verify it for validity, i.e. compliance with the URL format. After that there is no verification.

If a server does not recognize a certain URL, it will typically generate a 404 error, which may or may not be the recognition of its validity, because some URL-s can simply expire, some may fail to be served because of a server problem at the moment, some may simply not like your locale or your IP address, etc...

Calling InternetCheckConnection isn't gonna help you much here, it mainly does what it says, checks your internet connection, but this isn't what you inquired about. Although it does verify whether it can open the URL, it is secondary for the call, and may fail, for instance, on URL format validation. But even if it succeeded, it would be no indication of the URL being valid for reasons already described above.

You need to narrow your search to something more meaningful.

Examples:


  1. If you want to verify that the URL refers to an existing file, try to open and read that file.
  2. If you want to analyze the server response to the URL, just read and parse the response.
  3. If you are dealing with a custom URL that you expect in a certain format - check it locally.

and so on. And if you publish a new, better defined question, I'd be happy to answer that one ;)
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 24-Jun-12 21:50pm    
Agree, and you make good points, a 5.
--SA
[no name] 27-Jun-12 9:54am    
Thanks very much for your help.
I have found this piece of code, which checks the give website url is available.

bool CInternet::WebsiteAvailable(char* sURL)
{
// Declare variables
bool bAvailable = false;
CInternetSession * pNetSession = NULL;
CHttpConnection * pConnection = NULL;
CHttpFile * pFile = NULL;
CString sServer, sObject;
INTERNET_PORT nPort;
DWORD dwServiceType, dwStatusCode;

// Try to get a filepointer
try
{
// Parse URL to get all data
if (!AfxParseURL(sURL, dwServiceType, sServer, sObject, nPort))
{
// Unable to parse website address
return false;
}

// Get internet session
pNetSession = new CInternetSession();

// Get connection
pConnection = pNetSession->GetHttpConnection(sServer, nPort);

// Get the file
pFile = pConnection->OpenRequest(_T("GET"), sObject, _T(""), 1, NULL, NULL,
INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RELOAD);

// Check if file is available
if (pFile != NULL)
{
// Try to send an empty request
pFile->SendRequest();

// Check the status code
pFile->QueryInfoStatusCode(dwStatusCode);
if (dwStatusCode != HTTP_STATUS_NOT_FOUND)
{
// Yes, file is reachable
bAvailable = true;
}
};

// Close connection
pFile->Close();
pNetSession->Close();
}
catch (CException * pEx)
{
// Delete exception
pEx->Delete();

// Be sure result is false
bAvailable = false;
}

// Return result
return bAvailable;
}

But it doesn't work for https url.
How should I do?
Have a look at HttpClientExample[^].

Pay attention to the use of WsOpenChannel[^], this where the application establishes a connection based on an url.

If you need to support older versions of Windows, try cURL and libcurl[^].

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-Jun-12 21:49pm    
Agree, a 5.
--SA
Espen Harlinn 25-Jun-12 3:25am    
Thank you, Sergey :-)
I knew in PHP function get_headers() for check URL, may be this can help.
 
Share this answer
 
Comments
[no name] 27-Jun-12 9:56am    
Thanks for your attention.
I have to check in C/C++.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900