2014-05-17 94 views
-3

我能夠從Tomcat服務器使用以下網址從我的瀏覽器訪問資源,如何使用此客戶端程序訪問tomcat資源?

http://localhost:8080/test/file1.xml 

請求頭:

Status Code:200 OK 
Request Headersview parsed 
GET /test/file1.xml HTTP/1.1 
Host: localhost:8080 
Connection: keep-alive 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36 
Accept-Encoding: gzip,deflate,sdch 
Accept-Language: en-US,en;q=0.8 
Response Headersview parsed 

響應頭:

HTTP/1.1 200 OK 
Server: Apache-Coyote/1.1 
Accept-Ranges: bytes 
ETag: W/"9-1399430961358" 
Last-Modified: Wed, 07 May 2014 02:49:21 GMT 
Content-Type: application/xml 
Content-Length: 9 
Date: Fri, 16 May 2014 16:37:10 GMT 

但我無法訪問使用下面的Windows客戶端程序相同的資源,你能讓我知道原因嗎?

#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 65536 


int __cdecl main(int argc, char **argv) 
{ 
    WSADATA wsaData; 
    SOCKET ConnectSocket = INVALID_SOCKET; 
    struct addrinfo *result = NULL, 
        *ptr = NULL, 
        hints; 
    char *sendbuf = "GET /test/file1.xml HTTP/1.1"; 
    char recvbuf[DEFAULT_BUFLEN]; 
    int iResult; 
    int recvbuflen = DEFAULT_BUFLEN; 


    // 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_UNSPEC; 
    hints.ai_socktype = SOCK_STREAM; 
    hints.ai_protocol = IPPROTO_TCP; 

    // Resolve the server address and port 
    iResult = getaddrinfo("127.0.0.1", "8080", &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); 
     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); 
     if (iResult == SOCKET_ERROR) { 
      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 
    iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0); 
    if (iResult == SOCKET_ERROR) { 
     printf("send failed with error: %d\n", WSAGetLastError()); 
     closesocket(ConnectSocket); 
     WSACleanup(); 
     return 1; 
    } 

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


    // Receive until the peer closes the connection 
    do { 

     iResult = recv(ConnectSocket, recvbuf, recvbuflen-1, 0); 
     if (iResult > 0){ 
      recvbuf[iResult]='\0'; 
      printf("Bytes received: %d, httpresponse: %s\n", iResult,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; 
} 

輸出爲:

c:\code>client.exe 
Bytes Sent: 28 
Connection closed 

回答

2

這應該工作。

要發送HTTP/1.0請求,

char *sendbuf = "GET /test/file1.xml HTTP/1.0\r\n\r\n "; 

發送HTTP 1.1請求,

char *sendbuf = "GET /test/file1.xml HTTP/1.1\r\nHost: localhost\r\n\r\n "; 

虛擬主機的概念允許多於一個網站一個系統或Web服務器上。 HTTP/1.1服務器支持所謂的虛擬主機。因此,在HTTP/1.1 GET請求中,必須包含名爲「主機」的請求標頭,以選擇其中一個虛擬主機。

相關問題