#include <stdio.h> #include <string.h> #include <stdlib.h> #include <winsock2.h> #pragma comment(lib, "ws2_32.lib") int geturl(char *url) { /****************解析URL,解析資源名、主機名********************/ const char *parseptr1; const char *parseptr2; char host[BUFSIZ]; char resource[BUFSIZ]; int len, i; parseptr2 = url; parseptr1 = strstr(parseptr2, "http://"); if(NULL == parseptr1) //解析用戶輸入URL形式如"www.baidu.com" 狀況 { parseptr1 = strchr(parseptr2, '/'); len = parseptr1 - parseptr2; if(NULL == parseptr1) //解析主機、資源名 { strcpy(host, parseptr2); strcpy(resource, "/"); } else //解析用戶輸入URL形式如"www.baidu.com/xxx"狀況 { for(i = 0; i < len; i++) { host[i] = parseptr2[i];//解析主機名 } host[len] = '\0'; for(i = 0; i < len; i++) { parseptr2++; } strcpy(resource, parseptr2);//解析資源名 } printf("host: %s\nresouce: %s\n", host, resource); } else { parseptr1 = strchr(parseptr2, ':'); //解析用戶輸入URL形式如"http://www.baidu.com"狀況 len = parseptr1 - parseptr2; printf("protocol: "); //解析協議 for(i = 0; i < len; i++) { printf("%c", parseptr2[i]); } for(i = 0; i < 3; i++) { parseptr1++; } parseptr2 = parseptr1; parseptr1 = strchr(parseptr2, '/'); len = parseptr1 - parseptr2; if(NULL == parseptr1) { strcpy(host, parseptr2); //解析主機名 strcpy(resource, "/"); //解析資源名 } else //解析用戶輸入URL形式如"http://www.baidu.com/xxx.."狀況 { for(i = 0; i < len; i++) { host[i] = parseptr2[i]; } host[len] = '\0'; parseptr2 = parseptr1; strcpy(resource, parseptr2); } printf("\nhost: %s\nresource: %s\n", host, resource); } /*****************建立socket************/ //初始化套接字 WSADATA wsaData; WSAStartup(MAKEWORD(2,2), &wsaData); //建立套接字 SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(sock == INVALID_SOCKET) { printf("Failed socket().\n"); WSACleanup(); return 0; } //設置socket參數 struct sockaddr_in sockAddr; memset(&sockAddr, 0, sizeof(sockAddr)); sockAddr.sin_family = AF_INET; sockAddr.sin_port = htons(80); //獲取主機名和地址信息 struct hostent *hp = gethostbyname(host); if(hp == NULL) { printf("Can not find host address.\n"); return 0; } sockAddr.sin_addr.s_addr = *((unsigned long *)hp->h_addr); //鏈接到服務器 if(connect(sock, (SOCKADDR *)&sockAddr, sizeof(sockAddr)) == -1) { printf("Failed connect().\n"); WSACleanup(); return 0; } /****************與服務器通訊,收發數據***************/ //準備發送數據 char request[BUFSIZ] = ""; //request = "GET " + resource + " HTTP/1.1\r\nHost:" + host + "\r\nConnection:Close\r\n\r\n"; strcat(request, "GET "); strcat(request, resource); strcat(request, " HTTP/1.1\r\nHost:"); strcat(request, host); strcat(request, "\r\nConnection:Close\r\n\r\n"); //發送數據 if(SOCKET_ERROR==send(sock, request, sizeof(request), 0)) { printf("Send error.\n"); closesocket(sock); return 0; } //接收數據 FILE *fp; char ch; static char pageBuf[BUFSIZ]; printf("Read: "); int ret; while(ret > 0) { ret = recv(sock, pageBuf, BUFSIZ, 0); printf("%s", pageBuf); if((fp = fopen("website.txt", "ab")) == NULL)//建立文件,並保存源碼到本地磁盤 { fprintf(stderr, "Error opening file.\n"); exit(1); } fputs(pageBuf, fp); fclose(fp); //strnset(pageBuf, '\0', BUFSIZ); } closesocket(sock); WSACleanup(); return 0; } int main(int argc, char *argv[]) { char url[256]; puts("Enter URL:"); scanf("%s", url); geturl(url); return 0; }