求一个C++的WWW客户机代码
2个回答
2012-06-12
展开全部
总是建立连接失败,我个人觉得可能是软件界面的输入框没有与代码中的变量关联起来,但是具体怎么回事我一直搞不明白,所以来这里请教下各位前辈,我是用VC中MFC创建的工程,代码是在其中2个文件中添加的,代码如下:
// WebClientDlg.cpp : implementation file
//
#include "stdafx.h"
#include "WebClient.h"
#include "WebClientDlg.h"
#include "winsock.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWebClientDlg dialog
CWebClientDlg::CWebClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CWebClientDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CWebClientDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CWebClientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CWebClientDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CWebClientDlg, CDialog)
//{{AFX_MSG_MAP(CWebClientDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_EN_CHANGE(IDC_EDIT2, OnChangeEdit2)
ON_BN_CLICKED(IDOK, OnBrowse)
ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)
ON_LBN_SELCHANGE(IDC_LIST2, OnSelchangeList2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWebClientDlg message handlers
BOOL CWebClientDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CWebClientDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CWebClientDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CWebClientDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CWebClientDlg::OnChangeEdit2()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
}
void CWebClientDlg::OnBrowse()
{
WSADATA WSAData; //建立套接字绑定
if(WSAStartup(MAKEWORD(2,2),&WSAData)!=0)
{
MessageBox("初始化Winsock失败!");
return;
}
Socket=socket(AF_INET,SOCK_STREAM,0); //创建连接套接字
if(Socket==INVALID_SOCKET)
{
MessageBox("创建Socket失败!");
WSACleanup();
return;
}
CString TempUrl; //从URL删除协议
GetDlgItemText(IDC_EDIT2, m_Url);
if(strstr(m_Url,"http://"))
TempUrl=m_Url.Mid(7,m_Url.GetLength()-7);
else
TempUrl=m_Url;
int UrlPos; //从URL提取主机名
CString HostName;
CString FilePath;
UrlPos=TempUrl.Find("/",0);
HostName=TempUrl.Mid(0,UrlPos);
FilePath=TempUrl.Mid(UrlPos,m_Url.GetLength()-UrlPos);
int IpAddress; //判断域名或IP地址
IpAddress=inet_addr(HostName);
if(IpAddress==INADDR_NONE)
{
hostent *pHostent=gethostbyname(HostName);
if(pHostent)
IpAddress=(*(in_addr*)pHostent->h_addr).s_addr;
}
sockaddr_in serveraddr; //定义套接字地址
memset(&serveraddr,0,sizeof(serveraddr));
serveraddr.sin_family=AF_INET;
serveraddr.sin_port=htons(80);
serveraddr.sin_addr.s_addr=IpAddress;
int nConnect; //与服务器建立连接
nConnect=connect(Socket,(sockaddr *)&serveraddr,sizeof(serveraddr));
if(nConnect==SOCKET_ERROR)
{
MessageBox("连接建立失败!");
closesocket(Socket);
WSACleanup();
return;
}
memset(Command,0,MAX_SIZE); //生成GET命令
memcpy(Command,"GET",4);
memcpy(Command+4,FilePath,strlen(FilePath));
memcpy(Command+4+strlen(FilePath),"HTTP/1.1\r\nHost:",17);
memcpy(Command+21+strlen(FilePath),HostName,strlen(HostName));
memcpy(Command+21+strlen(FilePath)+strlen(HostName),"\r\n\r\n",4);
int nSend; //发送GET命令
nSend=send(Socket,Command,strlen(Command),0);
if(nSend==SOCKET_ERROR)
{
MessageBox("Socket发送失败!");
closesocket(Socket);
WSACleanup();
return;
}
m_Http+=Command;
CString RecvBuf;
while(true)
{
memset(Respond,0,MAX_SIZE);
int nRecv; //接收GET响应
nRecv=recv(Socket,Respond,MAX_SIZE,0);
if(nRecv==SOCKET_ERROR)
{
MessageBox("Socket接收失败!");
closesocket(Socket);
WSACleanup();
return;
}
RecvBuf+=Respond;
if(strstr(Respond,"</html>\r\n"))
break;
}
CString RespCode; //从响应中解析响应码
RespCode=RecvBuf.Mid(9,3);
int HtmlPos; //从响应码中提取HTML
CString RespText;
CString HtmlText;
if(atoi(RespCode)==200)
{
HtmlPos=RecvBuf.Find("<html>",0);
RespText=RecvBuf.Mid(0,HtmlPos);
HtmlText=RecvBuf.Mid(HtmlPos,RecvBuf.GetLength()-HtmlPos);
}
else
{
MessageBox("GET响应错误!");
closesocket(Socket);
WSACleanup();
return;
}
m_Http+=RespText;
m_Html+=HtmlText;
UpdateData(false);
closesocket(Socket);
WSACleanup(); //释放套接字绑定
// TODO: Add your control notification handler code here
}
void CWebClientDlg::OnSelchangeList1()
{
// TODO: Add your control notification handler code here
}
void CWebClientDlg::OnSelchangeList2()
{
// TODO: Add your control notification handler code here
}
// WebClientDlg.h : header file
//
#if !defined(AFX_WEBCLIENTDLG_H__A4694971_F8DD_4794_A2A5_2926B1461920__INCLUDED_)
#define AFX_WEBCLIENTDLG_H__A4694971_F8DD_4794_A2A5_2926B1461920__INCLUDED_
#define MAX_SIZE 4096
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
/////////////////////////////////////////////////////////////////////////////
// CWebClientDlg dialog
class CWebClientDlg : public CDialog
{
// Construction
public:
CWebClientDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CWebClientDlg)
enum { IDD = IDD_WEBCLIENT_DIALOG };
CString m_Url;
CString m_Http;
CString m_Html;
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CWebClientDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
//{{AFX_MSG(CWebClientDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnChangeEdit2();
afx_msg void OnBrowse();
afx_msg void OnSelchangeList1();
afx_msg void OnSelchangeList2();
//}}AFX_MSG
DECLARE_MESSAGE_MAP();
private:
char Command[MAX_SIZE];
char Respond[MAX_SIZE];
bool RespondStatue;
SOCKET Socket;
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_WEBCLIENTDLG_H__A4694971_F8DD_4794_A2A5_2926B1461920__INCLUDED_)
// WebClientDlg.cpp : implementation file
//
#include "stdafx.h"
#include "WebClient.h"
#include "WebClientDlg.h"
#include "winsock.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWebClientDlg dialog
CWebClientDlg::CWebClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CWebClientDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CWebClientDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CWebClientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CWebClientDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CWebClientDlg, CDialog)
//{{AFX_MSG_MAP(CWebClientDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_EN_CHANGE(IDC_EDIT2, OnChangeEdit2)
ON_BN_CLICKED(IDOK, OnBrowse)
ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)
ON_LBN_SELCHANGE(IDC_LIST2, OnSelchangeList2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWebClientDlg message handlers
BOOL CWebClientDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CWebClientDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CWebClientDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CWebClientDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CWebClientDlg::OnChangeEdit2()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
}
void CWebClientDlg::OnBrowse()
{
WSADATA WSAData; //建立套接字绑定
if(WSAStartup(MAKEWORD(2,2),&WSAData)!=0)
{
MessageBox("初始化Winsock失败!");
return;
}
Socket=socket(AF_INET,SOCK_STREAM,0); //创建连接套接字
if(Socket==INVALID_SOCKET)
{
MessageBox("创建Socket失败!");
WSACleanup();
return;
}
CString TempUrl; //从URL删除协议
GetDlgItemText(IDC_EDIT2, m_Url);
if(strstr(m_Url,"http://"))
TempUrl=m_Url.Mid(7,m_Url.GetLength()-7);
else
TempUrl=m_Url;
int UrlPos; //从URL提取主机名
CString HostName;
CString FilePath;
UrlPos=TempUrl.Find("/",0);
HostName=TempUrl.Mid(0,UrlPos);
FilePath=TempUrl.Mid(UrlPos,m_Url.GetLength()-UrlPos);
int IpAddress; //判断域名或IP地址
IpAddress=inet_addr(HostName);
if(IpAddress==INADDR_NONE)
{
hostent *pHostent=gethostbyname(HostName);
if(pHostent)
IpAddress=(*(in_addr*)pHostent->h_addr).s_addr;
}
sockaddr_in serveraddr; //定义套接字地址
memset(&serveraddr,0,sizeof(serveraddr));
serveraddr.sin_family=AF_INET;
serveraddr.sin_port=htons(80);
serveraddr.sin_addr.s_addr=IpAddress;
int nConnect; //与服务器建立连接
nConnect=connect(Socket,(sockaddr *)&serveraddr,sizeof(serveraddr));
if(nConnect==SOCKET_ERROR)
{
MessageBox("连接建立失败!");
closesocket(Socket);
WSACleanup();
return;
}
memset(Command,0,MAX_SIZE); //生成GET命令
memcpy(Command,"GET",4);
memcpy(Command+4,FilePath,strlen(FilePath));
memcpy(Command+4+strlen(FilePath),"HTTP/1.1\r\nHost:",17);
memcpy(Command+21+strlen(FilePath),HostName,strlen(HostName));
memcpy(Command+21+strlen(FilePath)+strlen(HostName),"\r\n\r\n",4);
int nSend; //发送GET命令
nSend=send(Socket,Command,strlen(Command),0);
if(nSend==SOCKET_ERROR)
{
MessageBox("Socket发送失败!");
closesocket(Socket);
WSACleanup();
return;
}
m_Http+=Command;
CString RecvBuf;
while(true)
{
memset(Respond,0,MAX_SIZE);
int nRecv; //接收GET响应
nRecv=recv(Socket,Respond,MAX_SIZE,0);
if(nRecv==SOCKET_ERROR)
{
MessageBox("Socket接收失败!");
closesocket(Socket);
WSACleanup();
return;
}
RecvBuf+=Respond;
if(strstr(Respond,"</html>\r\n"))
break;
}
CString RespCode; //从响应中解析响应码
RespCode=RecvBuf.Mid(9,3);
int HtmlPos; //从响应码中提取HTML
CString RespText;
CString HtmlText;
if(atoi(RespCode)==200)
{
HtmlPos=RecvBuf.Find("<html>",0);
RespText=RecvBuf.Mid(0,HtmlPos);
HtmlText=RecvBuf.Mid(HtmlPos,RecvBuf.GetLength()-HtmlPos);
}
else
{
MessageBox("GET响应错误!");
closesocket(Socket);
WSACleanup();
return;
}
m_Http+=RespText;
m_Html+=HtmlText;
UpdateData(false);
closesocket(Socket);
WSACleanup(); //释放套接字绑定
// TODO: Add your control notification handler code here
}
void CWebClientDlg::OnSelchangeList1()
{
// TODO: Add your control notification handler code here
}
void CWebClientDlg::OnSelchangeList2()
{
// TODO: Add your control notification handler code here
}
// WebClientDlg.h : header file
//
#if !defined(AFX_WEBCLIENTDLG_H__A4694971_F8DD_4794_A2A5_2926B1461920__INCLUDED_)
#define AFX_WEBCLIENTDLG_H__A4694971_F8DD_4794_A2A5_2926B1461920__INCLUDED_
#define MAX_SIZE 4096
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
/////////////////////////////////////////////////////////////////////////////
// CWebClientDlg dialog
class CWebClientDlg : public CDialog
{
// Construction
public:
CWebClientDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CWebClientDlg)
enum { IDD = IDD_WEBCLIENT_DIALOG };
CString m_Url;
CString m_Http;
CString m_Html;
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CWebClientDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
//{{AFX_MSG(CWebClientDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnChangeEdit2();
afx_msg void OnBrowse();
afx_msg void OnSelchangeList1();
afx_msg void OnSelchangeList2();
//}}AFX_MSG
DECLARE_MESSAGE_MAP();
private:
char Command[MAX_SIZE];
char Respond[MAX_SIZE];
bool RespondStatue;
SOCKET Socket;
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_WEBCLIENTDLG_H__A4694971_F8DD_4794_A2A5_2926B1461920__INCLUDED_)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
AiPPT
2024-09-19 广告
2024-09-19 广告
作为北京饼干科技有限公司的工作人员,关于AIPPT免费生成PPT的功能,我可以简要介绍如下:AIPPT是一款基于人工智能技术的PPT制作工具,它为用户提供了免费生成PPT的便捷服务。用户只需简单输入PPT的主题或内容大纲,AIPPT便能智能...
点击进入详情页
本回答由AiPPT提供
展开全部
// WebClientDlg.h : header file
#define MAX_SIZE 4096
class CWebClientDlg : public CDialog
{
public:
//{{AFX_DATA(CWebClientDlg)
enum { IDD = IDD_WEBCLIENT_DIALOG };
CString m_Html;
CString m_Http;
CString m_Url;
//}}AFX_DATA
protected:
//{{AFX_MSG(CWebClientDlg)
afx_msg void OnBrowse();
//}}AFX_MSG
private:
char Command[MAX_SIZE];
char Respond[MAX_SIZE];
bool RespondStatue;
SOCKET Socket;
};
// WebClientDlg.cpp : implementation file
#include "stdafx.h"
#include "WebClient.h"
#include "WebClientDlg.h"
#include "winsock.h"
void CWebClientDlg::OnBrowse()
{
UpdateData(true);
WSADATA WSAData; //建立套接字绑定
if(WSAStartup(MAKEWORD(2,2),&WSAData)!=0)
{
MessageBox("初始化Winsock失败!");
return;
}
Socket=socket(AF_INET,SOCK_STREAM,0); //创建连接套接字
if(Socket==INVALID_SOCKET)
{
MessageBox("创建Socket失败!");
WSACleanup();
return;
}
CString TempUrl; //从URL删除协议
if(strstr(m_Url,"http://"))
TempUrl=m_Url.Mid(7,m_Url.GetLength()-7);
else
TempUrl=m_Url;
int UrlPos; //从URL提取主机名
CString HostName;
CString FilePath;
UrlPos=TempUrl.Find("/",0);
HostName=TempUrl.Mid(0,UrlPos);
FilePath=TempUrl.Mid(UrlPos,m_Url.GetLength()-UrlPos);
int IpAddress; //判断域名或IP地址
IpAddress =inet_addr(HostName);
if(IpAddress==INADDR_NONE)
{
hostent* pHostent=gethostbyname(HostName);
if(pHostent)
IpAddress=(*(in_addr*)pHostent->h_addr).s_addr;
}
sockaddr_in serveraddr; //定义套接字地址
memset(&serveraddr,0,sizeof(serveraddr));
serveraddr.sin_family=AF_INET;
serveraddr.sin_port=htons(80);
serveraddr.sin_addr.s_addr=IpAddress;
int nConnect; //与服务器建立连接
nConnect=connect(Socket,(sockaddr*)&serveraddr,sizeof(serveraddr));
if(nConnect==SOCKET_ERROR)
{
MessageBox("连接建立失败!");
closesocket(Socket);
WSACleanup();
return;
}
memset(Command,0,MAX_SIZE); //生成GET命令
memcpy(Command,"GET ",4);
memcpy(Command+4,FilePath,strlen(FilePath));
memcpy(Command+4+strlen(FilePath)," HTTP/1.1\r\nHost: ",17);
memcpy(Command+21+strlen(FilePath),HostName,strlen(HostName));
memcpy(Command+21+strlen(FilePath)+strlen(HostName),"\r\n\r\n",4);
int nSend; //发送GET命令
nSend=send(Socket,Command,strlen(Command),0);
if(nSend==SOCKET_ERROR)
{
MessageBox("Socket发送失败!");
closesocket(Socket);
WSACleanup();
return;
}
m_Http+=Command;
CString RecvBuf;
while(true)
{
memset(Respond,0,MAX_SIZE);
int nRecv; //接收GET响应
nRecv=recv(Socket,Respond,MAX_SIZE,0);
if(nRecv==SOCKET_ERROR)
{
MessageBox("Socket接收失败!");
closesocket(Socket);
WSACleanup();
return;
}
RecvBuf+=Respond;
if(strstr(Respond,"</html>"))
break;
}
CString RespCode; //从响应解析响应码
RespCode=RecvBuf.Mid(9,3);
int HtmlPos; //从响应中提取HTML
CString RespText;
CString HtmlText;
if(atoi(RespCode)==200)
{
HtmlPos=RecvBuf.Find("<html>",0);
RespText=RecvBuf.Mid(0,HtmlPos);
HtmlText=RecvBuf.Mid(HtmlPos,RecvBuf.GetLength()-HtmlPos);
}
else
{
MessageBox("GET响应错误!");
closesocket(Socket);
WSACleanup();
return;
}
m_Http+=RespText;
m_Html+=HtmlText;
UpdateData(false);
closesocket(Socket);
WSACleanup(); //释放套接字绑定
}
#define MAX_SIZE 4096
class CWebClientDlg : public CDialog
{
public:
//{{AFX_DATA(CWebClientDlg)
enum { IDD = IDD_WEBCLIENT_DIALOG };
CString m_Html;
CString m_Http;
CString m_Url;
//}}AFX_DATA
protected:
//{{AFX_MSG(CWebClientDlg)
afx_msg void OnBrowse();
//}}AFX_MSG
private:
char Command[MAX_SIZE];
char Respond[MAX_SIZE];
bool RespondStatue;
SOCKET Socket;
};
// WebClientDlg.cpp : implementation file
#include "stdafx.h"
#include "WebClient.h"
#include "WebClientDlg.h"
#include "winsock.h"
void CWebClientDlg::OnBrowse()
{
UpdateData(true);
WSADATA WSAData; //建立套接字绑定
if(WSAStartup(MAKEWORD(2,2),&WSAData)!=0)
{
MessageBox("初始化Winsock失败!");
return;
}
Socket=socket(AF_INET,SOCK_STREAM,0); //创建连接套接字
if(Socket==INVALID_SOCKET)
{
MessageBox("创建Socket失败!");
WSACleanup();
return;
}
CString TempUrl; //从URL删除协议
if(strstr(m_Url,"http://"))
TempUrl=m_Url.Mid(7,m_Url.GetLength()-7);
else
TempUrl=m_Url;
int UrlPos; //从URL提取主机名
CString HostName;
CString FilePath;
UrlPos=TempUrl.Find("/",0);
HostName=TempUrl.Mid(0,UrlPos);
FilePath=TempUrl.Mid(UrlPos,m_Url.GetLength()-UrlPos);
int IpAddress; //判断域名或IP地址
IpAddress =inet_addr(HostName);
if(IpAddress==INADDR_NONE)
{
hostent* pHostent=gethostbyname(HostName);
if(pHostent)
IpAddress=(*(in_addr*)pHostent->h_addr).s_addr;
}
sockaddr_in serveraddr; //定义套接字地址
memset(&serveraddr,0,sizeof(serveraddr));
serveraddr.sin_family=AF_INET;
serveraddr.sin_port=htons(80);
serveraddr.sin_addr.s_addr=IpAddress;
int nConnect; //与服务器建立连接
nConnect=connect(Socket,(sockaddr*)&serveraddr,sizeof(serveraddr));
if(nConnect==SOCKET_ERROR)
{
MessageBox("连接建立失败!");
closesocket(Socket);
WSACleanup();
return;
}
memset(Command,0,MAX_SIZE); //生成GET命令
memcpy(Command,"GET ",4);
memcpy(Command+4,FilePath,strlen(FilePath));
memcpy(Command+4+strlen(FilePath)," HTTP/1.1\r\nHost: ",17);
memcpy(Command+21+strlen(FilePath),HostName,strlen(HostName));
memcpy(Command+21+strlen(FilePath)+strlen(HostName),"\r\n\r\n",4);
int nSend; //发送GET命令
nSend=send(Socket,Command,strlen(Command),0);
if(nSend==SOCKET_ERROR)
{
MessageBox("Socket发送失败!");
closesocket(Socket);
WSACleanup();
return;
}
m_Http+=Command;
CString RecvBuf;
while(true)
{
memset(Respond,0,MAX_SIZE);
int nRecv; //接收GET响应
nRecv=recv(Socket,Respond,MAX_SIZE,0);
if(nRecv==SOCKET_ERROR)
{
MessageBox("Socket接收失败!");
closesocket(Socket);
WSACleanup();
return;
}
RecvBuf+=Respond;
if(strstr(Respond,"</html>"))
break;
}
CString RespCode; //从响应解析响应码
RespCode=RecvBuf.Mid(9,3);
int HtmlPos; //从响应中提取HTML
CString RespText;
CString HtmlText;
if(atoi(RespCode)==200)
{
HtmlPos=RecvBuf.Find("<html>",0);
RespText=RecvBuf.Mid(0,HtmlPos);
HtmlText=RecvBuf.Mid(HtmlPos,RecvBuf.GetLength()-HtmlPos);
}
else
{
MessageBox("GET响应错误!");
closesocket(Socket);
WSACleanup();
return;
}
m_Http+=RespText;
m_Html+=HtmlText;
UpdateData(false);
closesocket(Socket);
WSACleanup(); //释放套接字绑定
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询