11月23日更新:
#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; } //接收数据 static char pageBuf[BUFSIZ]; printf("Read: "); int ret = 1; while(ret > 0) { ret = recv(sock, pageBuf, BUFSIZ, 0); printf("%s", pageBuf); 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; }
11月20日更新下面源代码:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <winsock2.h> #pragma comment(lib, "ws2_32.lib") int geturl(char *url) { /***********解析URL,解析出主机名,资源名**********/ char host[BUFSIZ]; char resource[BUFSIZ]; char myurl[BUFSIZ]; char *pHost = 0; if(strlen(url) > 2000) { return 0; } //解析出主机和资源名 strcpy(myurl, url); for(pHost = myurl; *pHost != '/' && *pHost != '\0'; ++pHost); if((int)(pHost - myurl) == strlen(myurl)) { strcpy(resource, "/"); } else { strcpy(resource, pHost); } *pHost = '\0'; strcpy(host, myurl); printf("%s\n%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; } //接收数据 static char pageBuf[BUFSIZ]; printf("Read: "); int ret = 1; while(ret > 0) { ret = recv(sock, pageBuf, BUFSIZ, 0); printf("%s", pageBuf); strnset(pageBuf, '\0', BUFSIZ); } closesocket(sock); WSACleanup(); return 0; } int main(void) { char url[256]; printf("http://"); scanf("%s", url); geturl(url); return 0; }