//获取用户信息 CString CMySock::GetMemberInfo(char requestUrl[],char Data[]) { char RequestData[1024]; strcpy(RequestData,Data); LPHOSTENT lphost= gethostbyname(requestUrl); sockaddr_in server; server.sin_family = AF_INET; //server.sin_addr.s_addr = ((LPIN_ADDR)lphost->h_addr)->s_addr; server.sin_addr.s_addr = inet_addr(requestUrl); server.sin_port = htons(this->m_Port); if (connect(this->m_ManagerSock, (struct sockaddr *)&server, sizeof(struct sockaddr)) == SOCKET_ERROR){ MessageBox(NULL,_T("连接失败,请重试!"),_T("错误"),MB_OK|MB_ICONINFORMATION); closesocket(this->m_ManagerSock); return NULL; } int retval= send(this->m_ManagerSock,RequestData,sizeof(RequestData)-1,0); char buffer[1024]={0}; CString strResult; //char resultBuffer[1024] = ""; while( SOCKET_ERROR!= retval && retval>0) { if (SOCKET_Select(this->m_ManagerSock)) { ZeroMemory(buffer,sizeof(buffer)); retval = recv(this->m_ManagerSock ,(char*)buffer,sizeof(buffer),0); strResult += buffer; } } return strResult; }
在另外一个函数中进行调用:
char sz[] = "GET /verfiy.php?type=memberinfo HTTP/1.1\n" "Host: 127.0.0.1\n" "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\n" "Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3\n" "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0\n\n\n"; CMySock mySock; char requestUrl[128] = "127.0.0.1"; CString memberInfo = mySock.GetMemberInfo(requestUrl,sz);问题:
默认情况下,请求消息头中的,Connection默认为keep-alive
所以只需要再构造请求头中再添加上一句:
"Connection: close\r\n"
就可以了。
另外,也可以通过apache的配置文件,将keepalive设置为off(不建议)
版权所有,欢迎转载,但转载请注明: 转载自 曾是土木人