-1

So, i was wondering if it is possible(most likely without the useage of a libary) to read the raw paste/or better say the html code of a website using c++. All the codes/tutorials i found wasn't really helpful, so i was wondering if some of you could help me out. I want to read of of this link:

http://pastebin.com/raw/93HGpGG0

that code seems to be what im looking for but im missing Network and Http class...

bool Http::Connect(YString addr)
{
    _socket = Network::CreateConnectSocket(addr, 53); // 53 is the port
    return _socket != INVALID_SOCKET;
}
int iResult;
SOCKET ConnectSocket = INVALID_SOCKET;

// holds address info for socket to connect to
struct addrinfo *result = NULL,
    *ptr = NULL,
    hints;

ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;  //TCP connection!!!

                                  //resolve server address and port
iResult = getaddrinfo(addr.c_str(), std::to_string(port).c_str(), &hints, &result);
if (iResult != 0)
{
    printf("Network::CreateSocket failed with %s as addr, and %i as port.\nError code: %i.\n", (char*)addr.c_str(), port, iResult);
    return INVALID_SOCKET;
}

for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {

    // Create a SOCKET for connecting to server
    ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);

    if (ConnectSocket == INVALID_SOCKET) {
        printf("Network::CreateSocket failed with error: %ld\n", WSAGetLastError());
        return INVALID_SOCKET;
    }

    // Connect to server.
    iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);

    if (iResult == SOCKET_ERROR)
    {
        closesocket(ConnectSocket);
        ConnectSocket = INVALID_SOCKET;
        printf("Network::CreateSocket failed the server is down... did not connect.\n");
    }
}

freeaddrinfo(result);

if (ConnectSocket == INVALID_SOCKET)
{
    printf("Network::CreateSocket failed.\n");
    return INVALID_SOCKET;
}

u_long iMode = 1;
iResult = ioctlsocket(ConnectSocket, FIONBIO, &iMode);
if (iResult == SOCKET_ERROR)
{
    printf("Network::CreateSocket ioctlsocket failed with error: %d\n", WSAGetLastError());
    closesocket(ConnectSocket);
    return INVALID_SOCKET;
}
char value = 1;
setsockopt(ConnectSocket, IPPROTO_TCP, TCP_NODELAY, &value, sizeof(value));
return ConnectSocket;

Winsock 2 Reading text from a URL this was the post i got the coding from!

Hopefully someone can help me out im searching for this since may xD

Community
  • 1
  • 1
Dafuqisthis
  • 1
  • 1
  • 6

2 Answers2

0

Just use a simple HTTP GET Request with your socket:

GET /raw/93HGpGG0 HTTP/1.1\r\nHost: www.pastebin.com\r\n\r\n

You can use Boost socket library.And also you can use POSIX or winsock for create socket without any library:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms737591%28v=vs.85%29.aspx

http://www.linuxhowtos.org/C_C++/socket.htm

MHz Code
  • 105
  • 2
  • 9
0

You are asking for a possible solution without using any libraries. This solution uses a library but it is relatively easy:

Download libcurl and open libcurl.sln in Visual Studio. Choose whichever configuration you want (debug or release, static or dynamic, etc) and whether you want 32-bit or 64-bit, and build it. Once it finishesbuilding, read the compiler output in Visual Studio to see where it created your .lib file. Cut that lib file, then create a new folder somewhere outside of the source code named "curl", and create a folder inside of that one called "lib", and paste the library there. Then, go back to the source code folder, and copy the entire include folder over to the curl folder.

Now, and after making sure you included the library, it's time to test the it. Create a new Visual Studio project, and copy this source code into the main cpp file:

#include <stdio.h>
#include <curl/curl.h>

int main(void)
{
    CURL *curl;
    CURLcode res;

    curl = curl_easy_init();
    if (!curl) { return -1; }

    curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");

    /* example.com is redirected, so we tell libcurl to follow redirection */
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

    /* Perform the request, res will get the return code */
    res = curl_easy_perform(curl);

    /* Check for errors */
    if (res != CURLE_OK)
    {
        fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
    }

    /* always cleanup */
    curl_easy_cleanup(curl);
    return 0;
}

Result:

<!doctype html>
<html>
<head>
<title>Example Domain</title>

<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style type="text/css">
body {
background-color: #f0f0f2;
margin: 0;
padding: 0;
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;

}
div {
width: 600px;
margin: 5em auto;
padding: 50px;
background-color: #fff;
border-radius: 1em;
}
a:link, a:visited {
color: #38488f;
text-decoration: none;
}
@media (max-width: 700px) {
body {
background-color: #fff;
}
div {
width: auto;
margin: 0 auto;
border-radius: 0;
padding: 1em;
}
}
</style>
<script type="text/javascript" src="http://gc.kis.scr.kaspersky-labs.com/1B74BD89-2A22-4B93-B451-1C9E1052A0EC/main.js" charset="UTF-8"></script><script type="text/javascript" src="http://gc.kis.scr.kaspersky-labs.com/1B74BD89-2A22-4B93-B451-1C9E1052A0EC/main.js" charset="UTF-8"></script></head>

<body>
<div>
<h1>Example Domain</h1>
<p>This domain is established to be used for illustrative examples in documents. You may use this
domain in examples without prior coordination or asking for permission.</p>
<p><a href="http://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>
Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104
  • i seem to understand, what you want from me and i downloaded the latest version of lib, but in the .tar.gz are so many .sln projects and idk what to use could you give me a more detailed tutorial on creating the .lib or send me yours, because everything else seems to be what i need :) thx alot – Dafuqisthis Aug 01 '16 at 19:16
  • Sure. [**This Link**](http://www.cplusplus.com/forum/windows/173738/) is how I learned it and had it to work. The discussion starts as very general question but then began (Post: Sep 24, 2015 at 3:36pm by user `Wyboth`) to get specific on just "_How to include LibCurl_" Then I successfully compiled a "URL Source Code Getter" function. So good luck :) – Khalil Khalaf Aug 01 '16 at 19:29
  • I can also send you the version I used if this did not work out for you. Just let me know how it goes and I will help you. I know how much frustrating it is when it comes to including stuff in VS! – Khalil Khalaf Aug 01 '16 at 19:37
  • jesus this looks complicated... why has it always to be hard -.- Do you maybe have a Skype or such or could you send me your .lib file!? – Dafuqisthis Aug 01 '16 at 19:44