如何查询来自vC++项目的http请求

问题描述:

我在写一个示例程序以从某个站点获取文件。 但它回复我的文件没有找到错误,我不知道如何制作一个http头。 任何人都可以帮我编写http头文件。 (printf(“%s \ n”,ptr-> ai_addr-> sa_data);) 但没有得到它如何打印IP地址??,我是这样做对吗?如何查询来自vC++项目的http请求

我的代码是: -

#define WIN32_LEAN_AND_MEAN 

#include <windows.h> 
#include <winsock2.h> 
#include <ws2tcpip.h> 
#include <stdlib.h> 
#include <stdio.h> 


#define WIN32_LEAN_AND_MEAN 

#include <windows.h> 
#include <winsock2.h> 
#include <ws2tcpip.h> 
#include <stdlib.h> 
#include <stdio.h> 


// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib 
#pragma comment (lib, "Ws2_32.lib") 
#pragma comment (lib, "Mswsock.lib") 
#pragma comment (lib, "AdvApi32.lib") 


#define DEFAULT_BUFLEN 512 
#define DEFAULT_PORT "80" 

int __cdecl main(int argc, char **argv) 
{ 
    WSADATA wsaData; 
    SOCKET ConnectSocket = INVALID_SOCKET; 
    struct addrinfo *result = NULL, 
        *ptr = NULL, 
        hints; 
    char *sendbuf = "GET/HTTP/1.1[CRLF] Host: www.espncricinfo.com[CRLF]Connection: close[CRLF]User-Agent: Web-sniffer/1.0.37 (+http://web-sniffer.net/)[CRLF]Accept-Encoding: gzip[CRLF]Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7[CRLF]Cache-Control: no-cache[CRLF]Accept-Language: de,en;q=0.7,en-us;q=0.3 \r\n"; 
    char buff[DEFAULT_BUFLEN],recvbuf[512]; 
    int iResult; 
    int recvbuflen = DEFAULT_BUFLEN; 

    // Validate the parameters 
    if (argc != 1) { 
     printf("usage: %s server-name\n", argv[0]); 
     return 1; 
    } 

    // Initialize Winsock 
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData); 
    if (iResult != 0) { 
     printf("WSAStartup failed with error: %d\n", iResult); 
     return 1; 
    } 

    ZeroMemory(&hints, sizeof(hints)); 
    hints.ai_family = AF_INET; 
    hints.ai_socktype = SOCK_STREAM; 
    hints.ai_protocol = IPPROTO_TCP; 
    // Resolve the server address and port 
    iResult = getaddrinfo("122.169.255.9",DEFAULT_PORT, &hints, &result); 
    if (iResult != 0) { 
     printf("getaddrinfo failed with error: %d\n", iResult); 
     WSACleanup(); 
     return 1; 
    } 

    // Attempt to connect to an address until one succeeds 
    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); 
     printf("inside loop %s ",ptr->ai_addr->sa_data); 
     if (ConnectSocket == INVALID_SOCKET) { 
      printf("socket failed with error: %ld\n", WSAGetLastError()); 
      WSACleanup(); 
      return 1; 
     } 

     // Connect to server. 
     iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen); 
     printf("%d\n",ptr->ai_addrlen); 
//  printf("%s\n",ptr->ai_addr->sa_family); 
     printf("%s\n",ptr->ai_addr->sa_data); 

     if (iResult == SOCKET_ERROR) { 
      printf("socket error\n"); 
      printf("failed with error: %d\n", WSAGetLastError()); 
      closesocket(ConnectSocket); 
      ConnectSocket = INVALID_SOCKET; 
      continue; 
     } 
     break; 
    } 

    freeaddrinfo(result); 

    if (ConnectSocket == INVALID_SOCKET) { 
     printf("Unable to connect to server!\n"); 
     WSACleanup(); 
     return 1; 
    } 
    // Send an initial buffer 
    char filepath[]="pakistan/content/current/story/517271.html"; 
    sprintf(buff,"GET %s\r\n",filepath); 
    iResult = send(ConnectSocket,buff, (int)strlen(buff), 0); 
    if (iResult == SOCKET_ERROR) 
    { 
     printf("send failed with error: %d\n", WSAGetLastError()); 
     closesocket(ConnectSocket); 
     WSACleanup(); 
     return 1; 
    } 

    printf("Bytes Sent: %ld\n", iResult); 

    // shutdown the connection since no more data will be sent 
    iResult = shutdown(ConnectSocket, SD_SEND); 
    if (iResult == SOCKET_ERROR) { 
     printf("shutdown failed with error: %d\n", WSAGetLastError()); 
     closesocket(ConnectSocket); 
     WSACleanup(); 
     return 1; 
    } 

    do 
    { 
     iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0); 
     //if (iResult > 0) printf("Bytes received: %d\n", iResult); 
     if (iResult > 0) 
     { 
      printf("Bytes received: %d\n", iResult); 
      printf("%s",recvbuf); 
     } 
     else if (iResult == 0) 
      printf("Connection closed\n"); 
     else 
      printf("recv failed with error: %d\n", WSAGetLastError()); 

    } while(iResult>0); 

    // cleanup 
    closesocket(ConnectSocket); 
    WSACleanup(); 

    return 0; 
} 

如果你要使用Microsoft Windows的功能,在WinInet中的HTTP的功能是方便很多。请参阅HttpOpenRequest/HttpSendRequest/HttpQueryInfo/InternetReadFile

+0

thnx你说的是对的,它很容易,我用它完成了我的工作...... :) – abhinav 2011-06-02 04:36:35

如果你不想推倒重来,使用图书馆像libcurl

+0

我只是想每次都从同一个网址下载1个文件,所以如果我可以指定标头它的罚款,我需要帮助创建我的标头.. :) – abhinav 2011-05-31 11:06:21

如果您收到“找不到文件”,这是因为您正在请求无法找到的文件。

也许尝试"/pakistan/content/current/story/517271.html",带领先斜线。

你的HTTP协议说明符在哪里?

写:

GET path HTTP/1.0 

(1.0,因为你可能不想招惹虚拟主机; 1.1,如果你要)

,为什么你关机尝试读取之前插座从中...?我知道你只关闭了“发送”管道,但它仍然看起来很奇怪。

也许你应该使用预先建立的库来为你做这个。


http://122.169.255.9/的网络服务器似乎并未实际工作。先用http://www.google.com等已知好的服务器进行测试。

+0

“GET/HTTP/1.0 \ r \ n Host:www.espncricinfo.com \ r \ n Connection:close \ r \ n Accept-Encoding:gzip \ r \ n Accept-Charset:ISO-8859-1,UTF-8; q = 0.7,*; q = 0.7 \ r \ n Accept-Language:de,en; q = 0.7,en-us; q = 0.3 \ r \ n“; ...................这是我的http标题,你可以纠正我的错误plss – abhinav 2011-05-31 11:29:39

+0

@abhinav:这不是你的问题所说的。 – 2011-05-31 11:32:16

+0

@tomalak:现在我的问题已经简化了,我刚刚将http头改为“GET/HTTP/1.0 \ r \ n”;我得到http响应,但当我发送http请求为“GET路径HTTP/1.0 \ r \ n”,我得到文件未找到,我使用80.168.92.185连接到cricinfo ...... – abhinav 2011-05-31 11:39:49

除了托默勒格Geret'kal

你会想,除非你要检查分块的内容和更多的东西HTTP 1.1可以在你扔使用HTTP/1.0。

安装Wireshark并记录请求。你会看到什么是错的。

我不知道的[CRLF]被自动改为用\ r \ n

+0

CRLF _is_'\ r \ n'。 – 2011-05-31 11:32:41

+0

哦,我明白了,他编辑了他的问题并使用了一个字符串'“[CRLF]”。大声笑 – 2011-05-31 11:41:10