我在这个套接字中错过了什么? (windows 个插座)
What i missed in this socket get with parameters? (windows sockets)
我在 windows 中有以下获取请求。
我想弄清楚为什么我没有收到回复。
我很确定错误出在获取请求字符串中。
#include <iostream>
#include <fstream>
#include <string>
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#pragma comment(lib,"ws2_32.lib")
#pragma warning(disable: 4996)//for winsock
int main() {
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
std::cout << "WSAStartup failed.\n";
system("pause");
return 1;
}
SOCKET Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
struct hostent *host;
host = gethostbyname("www.google.com");
SOCKADDR_IN SockAddr;
SockAddr.sin_port = htons(80);
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
std::cout << "Connecting...\n";
if (connect(Socket, (SOCKADDR*)(&SockAddr), sizeof(SockAddr)) != 0){
return 1;
}
char *httpRequest = "GET /get\r\nHost:www.google.com?uuid=2010010\r\nConnection: close\r\n\r\n";
send(Socket, httpRequest, strlen(httpRequest), 0);
char buffer[10000];
int nDataLength;
while ((nDataLength = recv(Socket, buffer, 10000, 0)) > 0){
int i = 0;
while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
i += 1;
}
}
if (nDataLength > 0)
std::cout << "Received GET response";
else
std::cout << "NOT GET response: size = 0";
std::cout<<buffer;
closesocket(Socket);
WSACleanup();
}
我在页面中得到的只是 html 代码...我不明白为什么我没有收到回复
您在请求的第一行缺少 HTTP 版本。
它应该以 HTTP/1.1
结尾
或者放弃版本和另一个 headers,只使用版本 0.9
我在 windows 中有以下获取请求。 我想弄清楚为什么我没有收到回复。
我很确定错误出在获取请求字符串中。
#include <iostream>
#include <fstream>
#include <string>
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#pragma comment(lib,"ws2_32.lib")
#pragma warning(disable: 4996)//for winsock
int main() {
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
std::cout << "WSAStartup failed.\n";
system("pause");
return 1;
}
SOCKET Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
struct hostent *host;
host = gethostbyname("www.google.com");
SOCKADDR_IN SockAddr;
SockAddr.sin_port = htons(80);
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
std::cout << "Connecting...\n";
if (connect(Socket, (SOCKADDR*)(&SockAddr), sizeof(SockAddr)) != 0){
return 1;
}
char *httpRequest = "GET /get\r\nHost:www.google.com?uuid=2010010\r\nConnection: close\r\n\r\n";
send(Socket, httpRequest, strlen(httpRequest), 0);
char buffer[10000];
int nDataLength;
while ((nDataLength = recv(Socket, buffer, 10000, 0)) > 0){
int i = 0;
while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
i += 1;
}
}
if (nDataLength > 0)
std::cout << "Received GET response";
else
std::cout << "NOT GET response: size = 0";
std::cout<<buffer;
closesocket(Socket);
WSACleanup();
}
我在页面中得到的只是 html 代码...我不明白为什么我没有收到回复
您在请求的第一行缺少 HTTP 版本。
它应该以 HTTP/1.1
或者放弃版本和另一个 headers,只使用版本 0.9