gpt4 book ai didi

c - 我如何使用此客户端程序访问 tomcat 资源?

转载 作者:行者123 更新时间:2023-11-29 00:02:37 26 4
gpt4 key购买 nike

我可以从我的浏览器使用以下 URL 从 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

最佳答案

这应该有效。

发送 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 ";

虚拟主机的概念允许在一个系统或网络服务器上有多个网站。 HTTP/1.1 服务器支持所谓的虚拟主机。因此,在 HTTP/1.1 GET 请求中,必须包含一个名为“Host”的请求 header ,以选择其中一个虚拟主机。

关于c - 我如何使用此客户端程序访问 tomcat 资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23713632/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com