oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Wed Oct 29, 2008 7:03 pm Post subject: Get Request Problem |
|
|
I'm trying to code a simple get request but I'm not receiving anything from wikipedia. It connects to Wikipedia but when I send the request I don't get anything back. Btw, what is an appropriate size for my buffer that is going to receive the HTML? I put 1000 even though that seems a bit too small.
| Code: |
#include <winsock2.h>
#include <ws2tcpip.h>
#include <conio.h>
#include <iostream>
#pragma comment(lib, "WS2_32.lib")
int main() {
char sendbuf[55] = "GET /wiki/Main_Page HTTP/1.1\r\nHost: en.wikipedia.org\r\n";
char recvbuf[1000];
int Result = 0;
SOCKET ConnectSocket;
struct addrinfo hints, *result, *ptr;
WSADATA wsaData;
if(WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
std::cout<<"WSAStartup failed with error: "<<WSAGetLastError();
_getch();
return 1;
}
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
if(getaddrinfo("en.wikipedia.org", "80", &hints, &result) != 0) {
std::cout<<"getaddrinfo failed with error: "<<WSAGetLastError();
WSACleanup();
_getch();
return 1;
}
for(ptr = result; ptr != NULL; ptr = ptr->ai_next) {
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if(ConnectSocket == INVALID_SOCKET) {
std::cout<<"socket failed with error: "<<WSAGetLastError();
freeaddrinfo(result);
WSACleanup();
_getch();
return 1;
}
if(connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen) != SOCKET_ERROR) {
std::cout<<"Connected successfully!\n\n";
break;
}
else {
std::cout<<"connect failed with error: "<<WSAGetLastError();
freeaddrinfo(result);
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
continue;
}
}
freeaddrinfo(result);
send(ConnectSocket, sendbuf, strlen(sendbuf), NULL);
Result = recv(ConnectSocket, recvbuf, 512, NULL);
recvbuf[Result] = '\0';
std::cout<<"Dump of http://en.wikipedia.org/wiki/Main_Page: \n\n"<<recvbuf<<"\n\n";
shutdown(ConnectSocket, SD_BOTH);
closesocket(ConnectSocket);
WSACleanup();
std::cin.sync();
std::cin.ignore();
return 0;
}
|
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|