Verify existing url winsock2

Hello cplusplus.com Long time no see! ...

Okay Okay ... I need help here , I wonder how I make a simple function that does a scan of any url and tell me if this url exists or not.

How this would be done using winsock2.h the dev-C + +?
You need to try to talk to the URL. If you fail to connect to the host or get an error when trying to access the URL, there's a problem.

The easiest way is to use a library. Boost asio tcp streams provide a very simple interface to using TCP connections.
http://www.boost.org/doc/libs/1_47_0/doc/html/boost_asio/overview/networking/iostreams.html

You can do it directly using sockets:
1. connect to the server in the normal way
2. send the HTTP header as shown in the Boost example above.
I'd use libcurl instead of Boost for this task, but is a matter of pwesonal preference.
You really should use a third party library for this and I'd like to add that SFML is another valid option.

But, in the interest of academia, you would fill in a 'addrinfo' struct using the "getaddrinfo()" function. The first argument will be the address of the site that you are trying to connect to and the second argument should be "http" to indicate the service. Then use that 'addrinfo' sturct to create a SOCKET with the function "socket()" and finally call "connect()" to establish a connection. If the return value from "connect()" is not 'SOCKET_ERROR' then you have a valid connection.
Hey I´m sorry for the slow to respond...

I managed to use the library boost C++ successfully.

but... , I get verify only "this-site.com"<by example>

How get verify "this-site.com/dir1/dir2/source.html"?
Connect to this-site.com and send get request for /dir1/dir2/source.html
yes naraku9333, How connect this "this-site.com" and send get request for "/dir1/dir2/source.html"?How can I do this?
This is what I use, adapted from a boost example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
std::string sv::util::get_http_data(const std::string& server, const std::string& file)
{
	try
	{
		boost::asio::ip::tcp::iostream sock(server, "http");
		sock.expires_from_now(boost::posix_time::seconds(60));

		if (!sock){ throw "Unable to connect: " +sock.error().message(); }

		// ask for the file
        sock << "GET /" << file << " HTTP/1.0\r\n" << "Host: " 
            << server << "\r\n" << "Accept: */*\r\n" << "Connection: close\r\n\r\n";		

		// Check that response is OK.
		std::string http_version;
        unsigned int status_code;
		sock >> http_version >> status_code;

		std::string status_message;
		std::getline(sock, status_message);
		if (!sock && http_version.substr(0, 5) != "HTTP/"){ throw "Invalid response\n"; }
		if (status_code != 200){ throw "Response returned with status code " + status_code + status_message; }

		// Process the response headers, which are terminated by a blank line.
		std::string header;
		while (std::getline(sock, header) && header != "\r"){}
		
        // Write the remaining data to output.
        std::stringstream ss;
        ss << sock.rdbuf();
        return ss.str();
	}
	catch (std::exception& e)
	{
		return e.what();
	}
}
Topic archived. No new replies allowed.