如何用MFCSocket类实现Http协议客户端编程?

解决方案 »

  1.   

    就是一个tcp,自己分析判断一些传来传去的数据而已.在做显示工作.
    数据的格式就需要参考了.
      

  2.   

    头文件HEAD.H内容:
        #define IDM_STRAT 200
        #define IDM_EDIT 200
        class Mainwnd:public CFrame Wnd
        {public:Mainwnd();
        afx_msg int OnCreat(LPCREATESTRUCT);
        afx_msg void OnStart(void);
        DECLARE_MESSAGE_MAP();
        private:Cstatic CSStatic;
        CEdit LineEdit;
        CButten StartButton;};
        class PengApp:public CWinApp
        {public:BOOL InitInstance();}
        源程序Client.CPP清单:
        #include<afxwin.h>
        #include<winsock.h>
        #include "head.h"
        const int nPort=13;
        PengApp theApp;
        Main Wnd:Main Wnd()
        {if(!Create (NULL,"Communication Program",WS_OVERLAPPEDW
    INDOW,rectDefaul t)) AfxAbort();}
        int Mainwnd:OnCreate(LPCREATESTRUCT)
        {Rect rect;SetRect(& rect,80,50,160,70);
        Create("Host Name:",WS_CHILD|WS_VISIBLE|SS_LEFT,rect,thi
    s);
        SetRect(& rect,60,80,180,100);
        LineEdit.Create(WS_CHILD|WS_VISIBLE|WS_DLGFRAME|ES_LEFT,
    rect,this,IDM_ED IT);
        SetRect(&rect,100,120,140,140);
        StartButton,Create("start",WS_CHILD|VS_VISIBLE|BS_PUSHBU
    TTON,rect,this,I DM_START);
        return 0;}
        BEGIN_MESSAGE_MAP(Main Wnd,CFrameWnd)
        ON_WM_CREATE()
        ON_BN_CLICKED(IDM_START,OnStart)
        END_MESSAGE_MAP()
        BOOL ControlApp:InitInstance()
        {m_pMainWnd=new Main Wnd();
        m_pMainWnd→ShowWindow (m_nCmdShow);
        m_pMainWnd→UpdateWindow();
        return;}
        Void Main Wnd:Onstart(void)
        {CSocket TimeClient;
        if(! AfxSocketInit()) MessageBox("WindowsSocket initial 
    failed!","Receiv e",MB_ICONSTOP);
        if(! TimeClient.Create()) MessageBox("ReceiveSocket crea
    te failed","Rece ive",MB_I(ON)STOP);
        else TimeClient.connect(strAddr,nPort);
        TimeClient.ReceiveFrom(csReceiveText,csCounts,LineEdit.G
    etWinText,nPort)
        MessageBox(TimeClient.csReceiveText);
        TimeClient.Close();}
      

  3.   

    see this
    http://www.codeproject.com/internet/HTTPGet-Post.asp
      

  4.   

    #include "afxinet.h"// 假设创建了一个用于HTTP CLIENT的类CQueryDlg
    // 里面有两个成员变量
    // CString m_host; 主机地址
    // CString m_out;  从WEB服务器获得的HTTP字符流
    // 其中:
    // m_host对应一个EDIT资源IDC_HOST
    // m_out对应一个EDIT资源IDC_OUT
    // IDC_HOST用于用户在运行时刻输入主机地址
    // IDC_OUT用于显示获得的HTTP字符流
    void CQueryDlg::OnQuery()
    {
        const CString http="http://";
        
        UpdateData(TRUE);
        m_out = "";
        UpdateData(FALSE);    TryURL(http + m_host);    TryURL(http+"www."+m_host);
    }void CQueryDlg::TryURL(CString URL)
    {
        CInternetSession session;    m_out+="Trying "+URL+"\r\n";
        UpdateData(FALSE);    CInternetFile *file=NULL;
        try
        {
            // We know for sure this is an internet file.
            // so the cast is safe
            file = (CInternetFile *)session.OpenURL(URL);
        }
        catch(CInternetException *pEx)
        {
            // if anything went wrong, just set file to NULL
            file = NULL;
            pEx->Delete();
        }    if (file)
        {
            m_out += "Connection established. \r\n";
            CString line;        for (int i=0; i<20 && file->ReadString(line); i++)
            {
                m_out += line + "\r\n";
            }
            file->Close();
            delete file;
        }
        else
        {
            m_out += "No server found there. \r\n";
        }    m_out += "--------------------------------";
        UpdateData(FALSE);
    }// 在这个例子中仅做了获得HTML字符流的的工作
    // 对HTML字符流的解释与显示工作需要另行设置。
    // 也可交给CHtmlView类来完成:)
    // 如果你需要这方面的代码,偶可以提供:)
      

  5.   

    http://www.codeproject.com/internet/HTTPGet-Post.asp