none
关于http请求的问题 RRS feed

  • 常规讨论

  • 用下面的程序只能请求道一个http头,怎么请求到页面内容?

    #include <windows.h>
    #include <stdio.h>
    #include <winsock.h>
    #pragma comment(lib,"Ws2_32")
    
    
    int strncasecmp(char* s1,char* s2,int len)
    {
    	for(int n=0;n<len;n++)
    	{
    		if(s1[n]!=s2[n])
    			return 1;
    	}
    	return 0;
    
    
    }
    
    
    
    
    
    
    SOCKET HTTPConnectToServer(char* server){
    SOCKADDR_IN serverInfo;
    SOCKET sck; 
    WSADATA wsaData; 
    LPHOSTENT hostEntry; 
    WSAStartup(MAKEWORD(2,2),&wsaData);
    hostEntry = gethostbyname(server);
    if(!hostEntry){ 
    WSACleanup(); 
    return 0; 
    } 
    sck = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(sck==INVALID_SOCKET){
    WSACleanup(); 
    puts("Failed to setup socket");
    getchar(); 
    return 0; 
    } 
    serverInfo.sin_family = AF_INET;
    serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list); 
    serverInfo.sin_port = htons(80); 
    int i = connect(sck,(LPSOCKADDR)&serverInfo,sizeof(struct sockaddr));
    
    if(sck==SOCKET_ERROR) return 0;
    if(i!=0) return 0;
    
    return sck;
    }
    
    
    
    
    
    
    
    
    
    
    void HTTPRequestPage(SOCKET s,char *page,char *host){
    unsigned int len;
    if(strlen(page)>strlen(host)){
    len=strlen(page);
    }else len = strlen(host);
    
    char *message=(char *)malloc(20+len);
    if(strlen(page)<=0){
    strcpy(message,"GET / HTTP/1.1\r\n");
    }else sprintf(message,"GET %s HTTP/1.1\r\n",page);
    send(s,message,strlen(message),0);
    
    memset(message,0,sizeof(message));
    sprintf(message,"Host: %s\r\n\r\n",host);
    send(s,message,strlen(message),0);
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    BOOL DownloadToBuffer(char * webpage,char * buffer,unsigned long max){
    if(webpage==NULL||buffer==NULL||max==0) return FALSE;
    
    unsigned short shift=0;
    if(strncasecmp(webpage,"http://",strlen("http://"))==0){
    shift=strlen("http://");
    }
    if(strncasecmp(webpage+shift,"www.",strlen("www."))==0){
    shift+=strlen("www.");
    }
    char *cut=(char *)malloc(strlen(webpage)-shift+1);
    strcpy(cut,strdup(webpage+shift));
    
    char *server = strtok(cut,"/");
    
    char *page = strdup(webpage+shift+strlen(server));
    
    SOCKET s = HTTPConnectToServer(server);
    HTTPRequestPage(s,page,server);
    
    int i = recv(s,buffer,max,0);
    closesocket(s);
    
    if(i<=0) return FALSE;
    return TRUE;
    }
    
    
    
    
    
    
    
    
    
    int main(int argc,char *argv[]){
    
    long	buflen=200000;
    char *buffer=(char*)malloc(buflen);
    memset(buffer,0,buflen);
    
    
    DownloadToBuffer("http://www.zeroplace.cn/attachments/month_0908/k200983016433.rar",buffer,buflen);
    
    printf(buffer);
    memset(buffer,0,sizeof(buffer));
    DownloadToBuffer("http://www.zeroplace.cn/attachments/month_0908/k200983016433.rar",buffer,sizeof(buffer));
    
    
    printf(buffer);
    
    
    
    printf("over\n");
    
    return 0;
    }
    
    2009年11月22日 8:07