2011-05-31 51 views
1

我在寫一個示例程序以從某個站點獲取文件。 但它回覆我的文件沒有找到錯誤,我不知道如何製作一個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; 
} 

回答

1

如果你不想推倒重來,使用圖書館像libcurl

+0

我只是想每次都從同一個網址下載1個文件,所以如果我可以指定標頭它的罰款,我需要幫助創建我的標頭.. :) – abhinav 2011-05-31 11:06:21

2

如果您收到「找不到文件」,這是因爲您正在請求無法找到的文件。

也許嘗試"/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

1

除了託默勒格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