如何用MFCSocket类实现Http协议客户端编程?
解决方案 »
- 头文件的相互包含问题
- WaitForSingleObject具体怎么用,请指点!
- 编译器问题???
- 请问如何编程察看另外一个程序开了多少进程?并且捕捉某一个进程发送和接收的数据包?
- 好想离开vc
- 主窗口启动后隐藏,子窗口启动,子窗口关闭时怎么让主窗口也关闭?
- 基于UDP下的可靠快速传输.(能详细点最好)
- checkyvc6 请进来拿分
- http://expert.csdn.net/Expert/topic/1129/1129187.xml?temp=.2162287说学编程的都是败类
- 简单问题,寻找最佳算法:a^2+b^2=c^2!
- 哪里可下载Bjarne的《The C++ Programming Language》中文或英文版?
- 怎样在Flexgrid控件的单元格上放置一个控件如EDIT BOX?
数据的格式就需要参考了.
#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();}
http://www.codeproject.com/internet/HTTPGet-Post.asp
// 里面有两个成员变量
// 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类来完成:)
// 如果你需要这方面的代码,偶可以提供:)