Discussion:
How to Checking a URL :Valid or Invalid ???
(too old to reply)
Rasheed
2007-07-31 07:25:38 UTC
Permalink
Hi All,
Could any body help me. Regarding how to check a URL is
Valid or
Invalid.
I am using Four functions to check the same.
1) hConnect = WinHttpConnect( hSession, "www.google.co.in",
INTERNET_DEFAULT_HTTPS_PORT, 0);
2) hRequest = WinHttpOpenRequest( hConnect,
"{GET}",
pUrl, /// "/sea123rch?
hl=en&q=flow123ers&meta="
NULL,
WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
WINHTTP_FLAG_BYPASS_PROXY_CACHE );
3) bResults = WinHttpSendRequest( hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS, 0,
WINHTTP_NO_REQUEST_DATA, 0,
0, 0 );
4) bResults = WinHttpReceiveResponse( hRequest, NULL );

Here is my observation when I have given the wrong URL in
WinHttpOpenRequest(..pUrl..), it has to throw the
ERROR_WINHTTP_INVALID_URL or at least WinHttpSendRequest() has to
thorw the Exception but I haven't getting this.


Would I have to set any flag to check URL is valid or not please if
any Sample code would be good.


Can any body please help me out to sort out this problem.


Here is the example for Valid and Invalid URL why:


Valid URL:
http://www.google.co.in/search?hl=en&q=flowers&meta= Gives me the
some data without any error message


but for Invalid URL:
http://www.google.co.in/sea123rch?hl=en&q=flow123ers&meta= will
give some error message that page cannt be open


so when i will get this error message (in the case of Invalid URL) I
have to set some flags in my code so i need to check this !!


Regards,
Rs.
Marcin Domaslawski
2007-08-02 12:40:36 UTC
Permalink
Hi,

Use
WinHttoQueryHeaders( hRequest, WINHTTP_QUERY_STATUS_CODE,
WINHTTP_HEADER_NAME_BY_INDEX, buffer_for_result, buffer_for_resp_size,
WINHTTP_NO_HEADER_INDEX );

When page doesnt exist in buffer_for_result you will get http status code:
http://msdn2.microsoft.com/en-us/library/aa383887.aspx

404 error code for page doesnt exist.

Marcin Domaslawski
Post by Rasheed
Hi All,
Could any body help me. Regarding how to check a URL is
Valid or
Invalid.
I am using Four functions to check the same.
1) hConnect = WinHttpConnect( hSession, "www.google.co.in",
INTERNET_DEFAULT_HTTPS_PORT, 0);
2) hRequest = WinHttpOpenRequest( hConnect,
"{GET}",
pUrl, /// "/sea123rch?
hl=en&q=flow123ers&meta="
NULL,
WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
WINHTTP_FLAG_BYPASS_PROXY_CACHE );
3) bResults = WinHttpSendRequest( hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS, 0,
WINHTTP_NO_REQUEST_DATA, 0,
0, 0 );
4) bResults = WinHttpReceiveResponse( hRequest, NULL );
Here is my observation when I have given the wrong URL in
WinHttpOpenRequest(..pUrl..), it has to throw the
ERROR_WINHTTP_INVALID_URL or at least WinHttpSendRequest() has to
thorw the Exception but I haven't getting this.
Would I have to set any flag to check URL is valid or not please if
any Sample code would be good.
Can any body please help me out to sort out this problem.
http://www.google.co.in/search?hl=en&q=flowers&meta= Gives me the
some data without any error message
http://www.google.co.in/sea123rch?hl=en&q=flow123ers&meta= will
give some error message that page cannt be open
so when i will get this error message (in the case of Invalid URL) I
have to set some flags in my code so i need to check this !!
Regards,
Rs.
Rasheed
2007-08-04 09:27:10 UTC
Permalink
Hi Marcin,
your given great and Appreciated information for me.
if possible Once again, I want your support or else any one, that i am
using WinHttpQueryHeaders( hRequest, WINHTTP_QUERY_STATUS_CODE,
WINHTTP_HEADER_NAME_BY_INDEX, NULL, lpdwHeaderLen,
WINHTTP_NO_HEADER_INDEX );
but still i am unable to catching HTTP_STATUS_NOT_FOUND error message
if(GetLastError() == HTTP_STATUS_NOT_FOUND)
{
Do Something;
}
but GetLastError() holds a value is
WINHTTP_OPTION_PASSPORT_RETURN_URL 87

And when i read the HTML Page, the contents are
{0x0019fc60 "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://
www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>The page cannot be found</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html;
charset=Windows-1252">
<STYLE type="text/css">
BODY { font: 8pt/12pt verdana }
H1 { font: 13pt/15pt verdana }
H2 { font: 8pt/12pt verdana }
A:link { color: red }
A:visited { color: maroon }
</STYLE>
</HEAD><BODY><TABLE width=500 border=0 cellspacing=10><TR><TD>

<h1>The page cannot be fo"}

By this page Information, I can see the <HTML><HEAD><TITLE>The page
cannot be found</TITLE> so why not i am getting
HTTP_STATUS_NOT_FOUND 404 // object not found error message.

If any more information please feel free to ask.

Thanks in Advance.
Regards,
Rs.
Marcin Domaslawski
2007-08-06 08:15:54 UTC
Permalink
Hi,

No - it doesn't work in this way. You set buffer on NULL so you dont care
about the status code value - and you should. Set an buffer to recieve value
of status code and after WinHttpQueryHeaders you should check value of
buffer.

LPVOID lpBuff = new WCHAR[ 8 ];
DWORD dwSize = 8;
WinHttpQueryHeaders( hRequest,
WINHTTP_QUERY_STATUS_CODE ,
WINHTTP_HEADER_NAME_BY_INDEX,
lpBuff,
&dwSize,
WINHTTP_NO_HEADER_INDEX );

If page is not available in lpBuff you will get 404 (check in debug).

Marcin Domaslawski
Post by Rasheed
Hi Marcin,
your given great and Appreciated information for me.
if possible Once again, I want your support or else any one, that i am
using WinHttpQueryHeaders( hRequest, WINHTTP_QUERY_STATUS_CODE,
WINHTTP_HEADER_NAME_BY_INDEX, NULL, lpdwHeaderLen,
WINHTTP_NO_HEADER_INDEX );
but still i am unable to catching HTTP_STATUS_NOT_FOUND error message
if(GetLastError() == HTTP_STATUS_NOT_FOUND)
{
Do Something;
}
but GetLastError() holds a value is
WINHTTP_OPTION_PASSPORT_RETURN_URL 87
And when i read the HTML Page, the contents are
{0x0019fc60 "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://
www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>The page cannot be found</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html;
charset=Windows-1252">
<STYLE type="text/css">
BODY { font: 8pt/12pt verdana }
H1 { font: 13pt/15pt verdana }
H2 { font: 8pt/12pt verdana }
A:link { color: red }
A:visited { color: maroon }
</STYLE>
</HEAD><BODY><TABLE width=500 border=0 cellspacing=10><TR><TD>
<h1>The page cannot be fo"}
By this page Information, I can see the <HTML><HEAD><TITLE>The page
cannot be found</TITLE> so why not i am getting
HTTP_STATUS_NOT_FOUND 404 // object not found error message.
If any more information please feel free to ask.
Thanks in Advance.
Regards,
Rs.
Rasheed
2007-08-06 15:14:25 UTC
Permalink
hi Marcin,
i am heartfull thanking you for quick around me.
but i am getting some hexvalue in the lpBuff,
if i converted into the DWORD Values is =16008896
if i converted into the char * Values is = is some symbole form.
if i am using GetLastError() values is 87;
but i am not able to get the Error HTTP 404 - File not found in the
lpBuff.

so can you please me to sort out this issue.

regards
Rs.
Marcin Domaslawski
2007-08-06 17:09:49 UTC
Permalink
Hi,

I back to your first post and checked that in fact with your code I get an
strange results. I wonder why you use INTERNET_DEFAULT_HTTPS_PORT flag to
WinHttpConnect. This flas is to https pages and you try http. Besides change
"{GET}" to "GET".

Marcin Domaslawski
Post by Rasheed
hi Marcin,
i am heartfull thanking you for quick around me.
but i am getting some hexvalue in the lpBuff,
if i converted into the DWORD Values is =16008896
if i converted into the char * Values is = is some symbole form.
if i am using GetLastError() values is 87;
but i am not able to get the Error HTTP 404 - File not found in the
lpBuff.
so can you please me to sort out this issue.
regards
Rs.
Rasheed
2007-08-07 04:29:46 UTC
Permalink
Hi ,
Sorry it has my copy paste mistake this is my function

hConnect = WinHttpConnect( hSession, "www.google.co.in",
80, 0);


port Number is- 80


regards
Rs
Marcin Domaslawski
2007-08-07 16:34:12 UTC
Permalink
Hi,

snippet of code:

HINTERNET hSession = NULL, hConnect = NULL, hRequest = NULL;

hSession = WinHttpOpen( NULL,
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, 0);
if ( NULL == hSession )
std::wcout << GetLastError();

hConnect = WinHttpConnect(hSession, L"www.google.co.in",
INTERNET_DEFAULT_HTTP_PORT, 0);

if (NULL == hConnect)
std::wcout << GetLastError();

hRequest = WinHttpOpenRequest( hConnect,
L"GET",
L"/sear99ch?hl=en&q=flowers&meta=",
NULL,
WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
0);
if (NULL == hRequest)
std::wcout << GetLastError();

if( ! WinHttpSendRequest( hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0,
WINHTTP_NO_REQUEST_DATA, 0, 0, 0 ) )

std::wcout << GetLastError();

if ( ! WinHttpReceiveResponse( hRequest, NULL ) )
std::wcout << GetLastError();

DWORD dwSize = 10;
LPVOID lpOutBuffer = new WCHAR[ 10 ];

WinHttpQueryHeaders( hRequest,
WINHTTP_QUERY_STATUS_CODE ,
WINHTTP_HEADER_NAME_BY_INDEX,
lpOutBuffer,
&dwSize,
WINHTTP_NO_HEADER_INDEX );

std::wcout << (wchar_t *)lpOutBuffer; // 404
// you can also get int res = _wtoi( (wchar_t *)lpOutBuffer );


Marcin Domaslawski
Post by Rasheed
Hi ,
Sorry it has my copy paste mistake this is my function
hConnect = WinHttpConnect( hSession, "www.google.co.in",
80, 0);
port Number is- 80
regards
Rs
Rasheed
2007-08-08 05:55:01 UTC
Permalink
Guru("Master") a tons thanks for you to resolve my Bug.

you are the Guru("Master") man ....


Regards,
Rs.
k***@gmail.com
2015-06-23 15:46:02 UTC
Permalink
Post by Rasheed
Guru("Master") a tons thanks for you to resolve my Bug.
you are the Guru("Master") man ....
Regards,
Rs.
Thanks Mr.Marcin, it helped me solve the same problem.THANKS a lot!!
Loading...