0
我試圖以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。每一行後。另外,HTTP1.1絕對不能在沒有Host頭的情況下工作,所以請停止嘗試,並根據您的最佳知識生成[mcve]。 –