// HttpSocket.h: interface for the CHttpSocket class.
//
//////////////////////////////////////////////////////////////////////#if !defined(AFX_HTTPSOCKET_H__F49A8F82_A933_41A8_AF47_68FBCAC4ADA6__INCLUDED_)
#define AFX_HTTPSOCKET_H__F49A8F82_A933_41A8_AF47_68FBCAC4ADA6__INCLUDED_#include "winsock2.h"
#include <afxinet.h>#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class _declspec(dllexport) CHttpSocket  
{
public:
int GetServerState(); //返回服务器状态码 -1表示不成功
int GetField(const char* szSession,char *szValue,int nMaxLength); //返回某个域值,-1表示不成功
int GetResponseLine(char *pLine,int nMaxLength); //获取返回头的一行
const char* GetResponseHeader(int &Length); //获取完整的返回头
const char * FormatRequestHeader(char *pServer,char *pObject,long &Length,
char* pCookie=NULL,char *pReferer=NULL,
long nFrom=0,long nTo=0,
int nServerType=0); //格式化请求头
int GetRequestHeader(char *pHeader,int nMaxLength) const;
BOOL SendRequest(const char* pRequestHeader = NULL,long Length = 0);

CHttpSocket();
virtual ~CHttpSocket(); BOOL SetTimeout(int nTime,int nType=0);
long Receive(char* pBuffer,long nMaxLength);
BOOL Connect(char* szHostName,int nPort=80);
BOOL Socket();
BOOL CloseSocket();protected:
char m_requestheader[1024]; //请求头
char m_ResponseHeader[1024]; //回应头
int m_port; //端口
char m_ipaddr[256]; //IP地址
BOOL m_bConnected;
SOCKET m_s;
hostent *m_phostent; int m_nCurIndex; //GetResponsLine()函数的游标记录
BOOL m_bResponsed; //是否已经取得了返回头
int m_nResponseHeaderSize; //回应头的大小
/*
int m_nBufferSize;
char *m_pBuffer;*/};#endif // !defined(AFX_HTTPSOCKET_H__F49A8F82_A933_41A8_AF47_68FBCAC4ADA6__INCLUDED_)-----------------------------------------------------------------------------------------// TestHttp.cpp : Defines the entry point for the console application.
//#include "stdafx.h"
#include <iostream.h>
#include "HttpSocket.h"
#include <windows.h>
int main(int argc, char* argv[])
{
cout<<"Please input the website address:";
char* m_strRequest=new char[200];
cin>>m_strRequest;// CTestHttpDlg *pMainWnd = (CTestHttpDlg *)pArg;
//CFileDialog FileDlg(FALSE);
char* strFileName="c:\\111.txt";
/* if(FileDlg.DoModal() == IDOK)
{
strFileName = FileDlg.GetPathName();
}
else
{
return 0;
}*/
CHttpSocket HttpSocket;
char* strServer,strObject;
unsigned short nPort;
DWORD dwServiceType;
long nLength;
const char *pRequestHeader = NULL;
AfxParseURL(m_strRequest,dwServiceType,strServer,strObject,nPort);

pRequestHeader = HttpSocket.FormatRequestHeader((LPTSTR)(LPCTSTR)strServer,(LPTSTR)(LPCTSTR)strObject,nLength);
HttpSocket.Socket();
HttpSocket.Connect((LPTSTR)(LPCTSTR)strServer);
HttpSocket.SendRequest();
HttpSocket.SetTimeout(10000,0);
/*
int nSize;
HttpSocket.GetResponseHeader(nSize);*/// pMainWnd->m_edtEdit1.SetWindowText(pRequestHeader);
cout<<endl<<pRequestHeader<<endl;
int nLineSize = 0;
char szLine[256];
while(nLineSize != -1)
{
nLineSize = HttpSocket.GetResponseLine(szLine,256);
if(nLineSize > -1)
{
szLine[nLineSize] = '\0';
//pMainWnd->m_ctrlList.AddString(szLine);
cout<<szLine<<endl;
}
}
char szValue[30];
HttpSocket.GetField("Content-Length",szValue,30);
int nSvrState = HttpSocket.GetServerState();
int nFileSize = atoi(szValue);
// pMainWnd->m_ctrlProgress.ShowWindow(SW_SHOW);
// pMainWnd->m_ctrlProgress.SetRange(0,nFileSize / 1024);
int nCompletedSize = 0;
CFile DownloadFile;
DownloadFile.Open(strFileName,CFile::modeCreate | CFile::modeWrite);
char pData[1024];
int nReceSize = 0;
DWORD dwStartTime,dwEndTime;
while(nCompletedSize < nFileSize)
{
dwStartTime = GetTickCount();
nReceSize = HttpSocket.Receive(pData,1024);
if(nReceSize == 0)
{
//AfxMessageBox("服务器已经关闭连接.");
cout<<服务器已经关闭连接<<endl;
break;
}
if(nReceSize == -1)
{
//AfxMessageBox("接收数据超时.");
cout<<接收数据超时<<endl;
break;
}
dwEndTime = GetTickCount();
DownloadFile.Write(pData,nReceSize);
nCompletedSize += nReceSize;
//pMainWnd->m_ctrlProgress.SetPos(nCompletedSize / 1024);

//Speed
//CString strSpeed;
//strSpeed.Format("%d",dwStartTime -dwEndTime);
//strSpeed.Format("%d",nReceSize);
//pMainWnd->m_stcSpeed.SetWindowText(strSpeed);
}
DownloadFile.Close();

//pMainWnd->m_ctrlProgress.ShowWindow(SW_HIDE);
//pMainWnd->m_ctrlProgress.SetPos(0);
return 0;
}
本程序编译时出现这样的错误,不知道为何,恳请大虾帮忙。谢谢。不知道如何修改。Deleting intermediate files and output files for project 'TestHttp - Win32 Debug'.
--------------------Configuration: TestHttp - Win32 Debug--------------------
Compiling...
StdAfx.cpp
Compiling...
TestHttp.cpp
d:\program files\vc98\mfc\include\afxv_w32.h(14) : fatal error C1189: #error :  WINDOWS.H already included.  MFC apps must not #include <windows.h>
Error executing cl.exe.TestHttp.exe - 1 error(s), 0 warning(s)

解决方案 »

  1.   

    不是说了么,不要include<windows.h>
    加#include<stdAfx.h>就够了
      

  2.   

    换一下顺序:
    #include <afxinet.h>   //在这之前不要出现windows.h
    #include "winsock2.h"
    #include "stdafx.h"
    #include <iostream.h>
    #include "HttpSocket.h"
    //#include <windows.h> 可以不要了,afxinet.h 和 winsock2.h 都有定义了
      

  3.   

    呵呵,windows.h和afxxxx.h是不能共用的
      

  4.   

    看下stdafx.h的内容就知道了,其中已经包含了window.h。再include就重复了。
      

  5.   

    MFC的应用程序中不应该包含Windows.h的,因为
    stdafx.h中包含了afxwin.h,afxwin.h已经包含了windows.h,而windows.h中没有处理重复包含的预处理宏一类的东东
      

  6.   

    老问题了,在包含afx.h之前不能包含windows.h
    解决方法是在stdafx.h中把windows.h放在afx.h下面#include<afx.h>
    #include<windows.h>之所以放在stdafx.h中,因为这个头通常在其它文件中也是第一个出现的。
    从而保证了它们的次序。