我正在尝试使用C套接字访问网站https://www.000webhost.com
:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char** argv) {
struct sockaddr_in servaddr;
struct hostent *hp;
int sock_id;
char message[1024*1024];
char request[] = "GET / HTTP/1.1\n" "From: ...\n";
//get a socket
if((sock_id = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
fprintf(stderr,"Couldn't get a socket.\n");
exit(EXIT_FAILURE);
}else {
fprintf(stderr,"Got a socket.\n");
}
memset(&servaddr,0,sizeof(servaddr));
//get address
if((hp = gethostbyname("000webhost.com")) == NULL) {
fprintf(stderr,"Couldn't get an address.\n");
exit(EXIT_FAILURE);
}else {
fprintf(stderr,"Got an address.\n");
}
memcpy((char *)&servaddr.sin_addr.s_addr, (char *)hp->h_addr, hp->h_length);
//port number and type
servaddr.sin_port = htons(80);
servaddr.sin_family = AF_INET;
//connect
if(connect(sock_id, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0) {
fprintf(stderr, "Couldn't connect.\n");
}else {
fprintf(stderr,"Got a connection.\n");
}
//request
write(sock_id,request,strlen(request));
//response
read(sock_id,message,1024*1024);
fprintf(stdout,"%s",message);
return 0;
}
如果将request[]
阵列从"GET / HTTP/1.1\n" "From: ...\n"
更改为"GET / HTTP/1.1\n" "Host: https://www.000webhost.com" "From: ...\n"
(因此从请求中删除了直接IP地址),我仍然会收到错误Error 1003. Direct IP access not allowed
。我需要修改请求的其他部分吗?我还需要做什么?
您的请求格式不正确:
每行都必须以\r\n
终止,而不仅仅是\n
。 (某些Web服务器会让您摆脱\n
的束缚,但我不知道它是否可以在000webhost上使用。)
我尝试使用http://104.26.8.55/来访问在学校被阻止的克鲁克。它总是说我无法访问它,因为它有一些cloudflare屎。有谁可以帮助我吗? HMU,网址为[email protected]