www.pudn.com > Jianasyhttpclient.rar > HttpAsySocket.cpp
// HttpAsySocket.cpp : implementation file
//
#include "stdafx.h"
#include "asyhttpclient.h"
#include "HttpAsySocket.h"
#include "MainFrm.h"
#include "asyhttpclientview.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CHttpAsySocket
CHttpAsySocket::CHttpAsySocket()
{ m_nDataLength=0;
m_nPos=0;
}
CHttpAsySocket::~CHttpAsySocket()
{
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CHttpAsySocket, CAsyncSocket)
//{{AFX_MSG_MAP(CHttpAsySocket)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0
/////////////////////////////////////////////////////////////////////////////
// CHttpAsySocket member functions
void CHttpAsySocket::OnClose(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
AfxMessageBox("will be stop!");
CAsyncSocket::OnClose(nErrorCode);
}
void CHttpAsySocket::OnConnect(int nErrorCode)
{
// ---------------新添--------------
//往输出缓冲准备请求信息
if (nErrorCode != 0)
{ AfxMessageBox("连接失败!");
Close();
OnClose(0);
return;
}
const TCHAR szHeaders[]=_T("Accept:*/*\r\nUser-Agent:HttpClient_Sample/1.0\r\n");
CMainFrame *pMainFrame;
pMainFrame=(CMainFrame *)AfxGetMainWnd();
//获取请求行的请求方法
CString strmethod;
if (pMainFrame->m_chkget)
strmethod="GET";
else
if (pMainFrame->m_chkpost)
strmethod="POST";
else
strmethod="HEAD";
//----将请求消息存入m_pBuffer-----
//生成请求行
m_pBuffer= strmethod+" "+pMainFrame->m_strObject+" HTTP/1.0\r\n";
//加入请求报头域
m_pBuffer+=szHeaders;
//加入实体报头域
//m_pBuffer+="Content-Type:application/x-www-form-urlencoded\r\n";
//m_pBuffer+="Content-Length: 23\r\n";
//m_pBuffer+="\r\n";
//加入实体
//m_pBuffer+="&op=showfull&ino=5519\r\n";
m_pBuffer+="\r\n";
//http://www.11688.com/job/cx_lei.asp?ypzw_id=900
m_nDataLength=strlen(m_pBuffer);
m_nPos=0;
//要求写通知
AsyncSelect(FD_WRITE|FD_CLOSE);
//CAsyncSocket::OnConnect(nErrorCode);
}
void CHttpAsySocket::OnReceive(int nErrorCode)
{ boolean bFirst;
// -------新添---------
if (0 != nErrorCode)
{
Close();
OnClose(0);
return;
}
//计算本次可接收字节数nFree
int n, nFree;
if (0 == m_nDataLength)
{
bFirst= true;
nFree = BUFFERSIZE;
} else
{
bFirst= false;
if (0 >= (nFree = BUFFERSIZE - m_nDataLength))
{ // Buffer be full, reset connect
Close();
OnClose(0);
return;
}
}
//在上次接收的数据之后,保存本次接收的数据,存入m_szBuf
if (0 > (n = Receive((void *)(m_szBuf+m_nDataLength), nFree)))
{
Close();
OnClose(0);
return;
};
//接收成功
m_nDataLength+=n; //保存在缓冲中数据的长度
m_szBuf[m_nDataLength]='\0';
//检查响应消息的状态行
//if bFirst
//else
//{};
//获取视中的编辑框
CMainFrame *pMainFrame;
CAsyhttpclientView *pEditView;
pMainFrame=(CMainFrame *)AfxGetMainWnd();
pEditView=(CAsyhttpclientView *)pMainFrame->GetActiveView();
CEdit &edit=pEditView->GetEditCtrl();
n=pEditView->GetWindowTextLength();
edit.SetSel(n,3500);
edit.ReplaceSel((char *)m_szBuf);
//清零
m_nDataLength=0;
m_nPos=0;
AsyncSelect(FD_READ|FD_CLOSE);
}
void CHttpAsySocket::OnSend(int nErrorCode)
{
// -----------新添---------------
if (nErrorCode != 0)
{
Close();
OnClose(0);
return;
}
int n;
long lEvent = FD_READ | FD_CLOSE;
if (0 < m_nDataLength)
{
if (0 > (n = Send(m_pBuffer, m_nDataLength)))
{
Close();
OnClose(0);
return;
} else
{
m_nPos += n;
m_nDataLength -= n;
if (0 < m_nDataLength) lEvent |= FD_WRITE;
}
}
AsyncSelect(lEvent);
// -----------新添---------------
CAsyncSocket::OnSend(nErrorCode);
}