⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpdelegate.cpp

📁 该程序是mobile5.0 ppc2003系统下的
💻 CPP
📖 第 1 页 / 共 3 页
字号:
			CStdioFile csf;
			
			///////////////////////////////////
			if(!csf.Open(_T("\\存储卡\\数字城管\\test.txt"),CFile::modeCreate|CFile::modeWrite | CFile::shareDenyWrite))
			{
				AfxMessageBox(L"写文件错误!\n文件正在使用中,请先关闭程序!",MB_ICONSTOP);
				//m_pHttpFile->Close();
				return FALSE;
			}
			int n=0;
			char *buf=new char[2048];
			//AfxMessageBox(L"开始读文件");
			while((n=m_pHttpFile->Read(buf,2048))>0)
			{
				csf.Write(buf,n);
			}
			csf.Close();
			delete []buf;
			//AfxMessageBox(L"文件下载成功!");
			
			///////////////////////////////////
		////////////////////////////////////
			if(!csf.Open(_T("\\存储卡\\数字城管\\test.txt"),CFile::modeRead | CFile::typeBinary | CFile::shareDenyWrite))
			{
				AfxMessageBox(L"读文件错误!\n文件正在使用中,请先关闭程序!",MB_ICONSTOP);			
			}
			else
			{
				char *xml=new char[409600];
				while((lensxml=csf.Read(tmps,2048))>0)
				{
					for(int rr=0;rr<lensxml;rr++)
					{
					xml[xmlsize+rr]=tmps[rr];
					}
					xmlsize+=lensxml;
				}
				//strcpy((char*)widexml,"");
				//memset(widexml,0,sizeof(widexml));
				//MultiByteToWideChar(CP_ACP, 0, xmlbuffer, -1, widexml, strlen(xmlbuffer));
				pHttpParams->m_Contents=xml;
				delete[] xml;
				csf.Close();
				wcscpy(widexml,_T(""));
				strcpy(tmps,"");
			}
	}
	return TRUE;
}

DWORD CHttpDelegate::GetHttpStatus(LPCTSTR lpServerName,LPCTSTR strObject,INTERNET_PORT nPort,BOOL bLinked)
{

	if(lpServerName  == NULL || strObject == NULL)return 0;
	//	INTERNET_PORT nPort = INTERNET_DEFAULT_HTTP_PORT;
	DWORD dwRet =  1;
	
	if (m_pSession == NULL) 
	{
		return dwRet;
	}
	
	m_pSession->m_strHttpSite.Format(L"%s%s",lpServerName,strObject);
	
	dwRet =  2;
	
	if(m_pHttpFile != NULL)
	{
		m_pHttpFile->Close();
		delete m_pHttpFile;
		m_pHttpFile=NULL;
	}
	// Picked a new server, close out connection and make a new one:
	/*	if (m_pConnection != NULL)
	{
	AfxMessageBox(L"GetHttpStatus  m_pConnection");
	m_pConnection->Close ();
	delete m_pConnection;
	m_pConnection = NULL;
}  */
	if(bLinked == TRUE)
	{
		//AfxMessageBox(_T("bLinked==true"));
		if (m_pConnection == NULL )
		{
			//AfxMessageBox(_T("m_pConnection == NULL"));
			m_pConnection = m_pSession->GetHttpConnection(lpServerName,nPort);
		}
		else
		{
			//AfxMessageBox(_T("m_pConnection != NULL"));
			m_pConnection->Close();
			delete m_pConnection;
			m_pConnection = NULL;
			m_pConnection = m_pSession->GetHttpConnection(lpServerName,nPort,_T("pda"),_T("pda"));
		}
	}
	if (m_pConnection == NULL )
	{
		//AfxMessageBox(_T("bLinked == TRUE##m_pConnection == NULL"));
		m_pConnection = m_pSession->GetHttpConnection(lpServerName,nPort);
		//	AfxMessageBox(_T("bLinked == FALSE##m_pConnection == NULL"));
	}	
	if (m_pConnection == NULL) 
	{		
		return dwRet;
	}

	
	m_pHttpFile = m_pConnection->OpenRequest(_T("POST"),strObject,NULL, 1, NULL, NULL, 
	INTERNET_FLAG_NO_AUTO_REDIRECT);//INTERNET_FLAG_EXISTING_CONNECT |  INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE|INTERNET_FLAG_NO_AUTO_REDIRECT|INTERNET_FLAG_SECURE
	m_pHttpFile->QueryInfoStatusCode(dwRet);
	//m_pHttpFile->QueryInfoStatusCode(dwRet);
	
	if(!m_pHttpFile->SendRequest())
	{
		
		m_pHttpFile->Close();
		
		return dwRet;
	}
	
	if(m_pHttpFile != NULL)
	{
		//AfxMessageBox(_T("hello"));
		m_pHttpFile->QueryInfoStatusCode(dwRet);
		
	}
	
	return dwRet;
}

BOOL CHttpDelegate::PostGetData(HttpParams * pHttpParams)
{
	//Sleep(30000);
	int   nRetCode = 0;
	//int len=0;
	DWORD dwAccessType = PRE_CONFIG_INTERNET_ACCESS;
	//wcscpy(widestr,_T(""));
	readfile(pHttpParams->m_filename);
	//MultiByteToWideChar(CP_ACP, 0, filebuffer, -1, widestr, strlen(filebuffer));
	//int lengfile=strlen(filebuffer);
	//int lengs=wcslen(widestr);
	
	/*
	CString strs=_T("---------------------------7d33a816d302b6\rContent-Disposition: form-data; name=\"file\"; filename=\"\\My Documents\\http.txt\"\r\nContent-Type: text/plain\r\n\r\nthis is a test\r\n---------------------------7d33a816d302b6--\r\n");
	strs=_T("---------------------------7d33a816d302b6\rContent-Disposition: form-data; name=\"file\"; filename=\"");
	strs+=pHttpParams->m_filename;
	strs+=_T("\"\r\nContent-Type: application/octet-stream\r\n\r\n");
	strs+=widestr;
	//strs+=_T("this is a test hahahahdadfkdioadfk测试");
	strs+=_T("\r\n---------------------------7d33a816d302b6--\r\n");

	int lens=strs.GetLength();
	*/
	//strcpy(datapart,"---------------------------7d33a816d302b6\rContent-Disposition: form-data; name=\"file\"; filename=\"");
	//char *file_name="";
	//WideCharToMultiByte(CP_ACP, 0,pHttpParams->m_filename , -1, file_name, pHttpParams->m_filename.GetLength(), NULL, NULL);
	//strcat(datapart,file_name);
	//strcat(datapart,"\"\r\nContent-Type: application/octet-stream\r\n\r\n");
	//strcat(datapart,filebuffer);
	strcpy(datapart,"");
	datahead=_T("---------------------------7d33a816d302b6\rContent-Disposition: form-data; name=\"file\"; filename=\"");
	datahead+=pHttpParams->m_filename;
	datahead+=_T("\"\r\nContent-Type: application/octet-stream\r\n\r\n");
	WideCharToMultiByte(CP_ACP, 0,datahead , -1, datapart, datahead.GetLength()*2, NULL, NULL);
	int lendatanow=strlen(datapart);
	//strcat(datapart,filebuffer);
	
	
	for(int k=0;k<filesize;k++)
	{

		datapart[lendatanow+k]=filebuffer[k];
	}
	//delete []filebuffer;
	char dataend[40];
	strcpy(dataend,"\r\n---------------------------7d33a816d302b6--\r\n\r\n");
	
	int lend=strlen(dataend);
	for(k=0;k<lend;k++)
	{
	datapart[lendatanow+filesize+k]=dataend[k];
	}
	
	//strcat(datapart,"\r\n---------------------------7d33a816d302b6--\r\n\r\n");
	//int lendatas=strlen(datapart);

	int lens=lendatanow+filesize+lend;
	//////////////////////////
	char str[20];	

	sprintf(str,"%d",lens);

	CString szHeaders=_T("Accept:*/*\r\nAccept-Language: GB2312\r\nContent-Type:multipart/form-data; boundary=---------------------------7d33a816d302b6\r\nContent-Length: ");
	CString headerstail=_T("\r\nConnection: Keep-Alive");
	szHeaders+=str;
	szHeaders+=headerstail;
	//AfxMessageBox(szHeaders);
	DWORD dwHttpRequestFlags = INTERNET_FLAG_NO_AUTO_REDIRECT;
	
	BOOL  bSuccess = TRUE;
	
	CMyInternetSession session(NULL, (DWORD)this, dwAccessType);
	TRY
	{
		// check to see if this is a reasonable URL

		CString strServerName=_T("");
		CString strObject=_T("");
		INTERNET_PORT nPort;
		DWORD dwServiceType;
		
		if (!AfxParseURL(pHttpParams->m_pszURL, dwServiceType, strServerName, strObject, nPort) || 
			dwServiceType != INTERNET_SERVICE_HTTP)
		{
			CInternetException* pEx = new CInternetException(kError1);
			THROW(pEx);
		}
		
		session.EnableStatusCallback(TRUE);
		
		m_pConnection = session.GetHttpConnection(strServerName, nPort);
		
		m_pHttpFile = m_pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject, NULL, (DWORD)this, NULL, NULL, dwHttpRequestFlags);
		//	m_pHttpFile->Write(_T("this is a"));
		if(m_pHttpFile->AddRequestHeaders(szHeaders,HTTP_ADDREQ_FLAG_ADD_IF_NEW)!=0)		
		{
		//AfxMessageBox(_T("add header is success"));
	//	}

		//long len=szformdata.GetLength();
		//if(m_pHttpFile->SendRequest(szHeaders,0,szformdata.GetBuffer(0),szformdata.GetLength()))

		
			
		//strcpy(filedata,"");
		//WideCharToMultiByte(CP_ACP, 0, strs, -1, filedata, strs.GetLength(), NULL, NULL);
		

		if(m_pHttpFile->SendRequest(szHeaders,0,datapart,lens))
		{
		//AfxMessageBox(_T("文件上传成功!"));
		}
		else
		{
		AfxMessageBox(_T("文件上传失败!"));

		}
		}
		
		DWORD dwRet;
		m_pHttpFile->QueryInfoStatusCode(dwRet);
		
		if (dwRet == HTTP_STATUS_DENIED)
		{
			
			return 0;
		}
		///////////////////////////////////////
		CString strNewLocation;
		m_pHttpFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, strNewLocation);
	//	AfxMessageBox(strNewLocation);

		// were we redirected?
		// these response status codes come from WININET.H
		
		if (dwRet == HTTP_STATUS_MOVED ||
			dwRet == HTTP_STATUS_REDIRECT ||
			dwRet == HTTP_STATUS_REDIRECT_METHOD)
		{
			
			CString strNewLocation=_T("");
			m_pHttpFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, strNewLocation);
			
			int nPlace = strNewLocation.Find(_T("Location: "));
			if (nPlace == -1)
			{
				CInternetException* pEx = new CInternetException(kError2);
				THROW(pEx);
			}
			
			strNewLocation = strNewLocation.Mid(nPlace + 10);
			nPlace = strNewLocation.Find('\n');
			if (nPlace > 0)
				strNewLocation = strNewLocation.Left(nPlace);
			
			// close up the redirected site
			
			//delete [] filedata;
			m_pHttpFile->Close();
			{
			delete m_pHttpFile;
			m_pHttpFile=NULL;
			}
			m_pConnection->Close();
			{
			delete m_pConnection;
			m_pConnection=NULL;
			}
			
			// figure out what the old place was
			if (!AfxParseURL(strNewLocation, dwServiceType, strServerName, strObject, nPort))
			{
				CInternetException* pEx = new CInternetException(kError2);
				THROW(pEx);
			}
			
			if (dwServiceType != INTERNET_SERVICE_HTTP)
			{
				CInternetException* pEx = new CInternetException(kError2);
				THROW(pEx);
			}
			
			// try again at the new location
			m_pConnection = session.GetHttpConnection(strServerName, nPort);
			m_pHttpFile = m_pConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET,
				strObject, NULL, (DWORD)this, NULL, NULL, dwHttpRequestFlags);
			m_pHttpFile->AddRequestHeaders(szHeaders);
			m_pHttpFile->SendRequest();
			
			m_pHttpFile->QueryInfoStatusCode(dwRet);
			if (dwRet != HTTP_STATUS_OK)
			{
				CInternetException* pEx = new CInternetException(kError2);
				THROW(pEx);
			}
		}
		
		TCHAR* szWEBPage = new TCHAR[MAX_WEBPAGE_SIZE+1];
		if(szWEBPage)
		{
			
			//AfxMessageBox(_T("here"));
				szWEBPage[0] = L'\0';
			
			TCHAR* sz     = new TCHAR[BUFFER_SIZE+1];
			TCHAR* szwBuf = new TCHAR[(BUFFER_SIZE+1)*2];
			
			sz[0] = L'\0';
			szwBuf[0] = L'\0';
			int n = 0;
			m_pHttpFile->SetReadBufferSize(BUFFER_SIZE*2);
			while (m_pHttpFile->ReadString(sz, BUFFER_SIZE))
			{
				wce_AsciiToWide(szwBuf, (const char*)sz);
				
				n += _tcslen(szwBuf);
				if(n >= MAX_WEBPAGE_SIZE)
					break;
				_tcscat(szWEBPage, szwBuf);
			}
			//AfxMessageBox(szWEBPage);
			delete [] sz;
			delete [] szwBuf;
		}
		
		delete [] szWEBPage;
		m_pHttpFile->Close();
		m_pConnection->Close();
	}
	CATCH (CInternetException,  pEx)
	{
		// catch things wrong with parameters, etc
		if (pEx->m_dwError < kErrorMin)
		{
			TCHAR szError[MAX_PATH]=TEXT("\0");
			pEx->GetErrorMessage(szError,MAX_PATH,NULL);
		}
		bSuccess = FALSE;
	}
	AND_CATCH (CMemoryException,  pMemory)
	{
		// catch things wrong with memor
		pMemory->Delete();
		bSuccess = FALSE;
	}
	END_CATCH_ALL
		
		if (m_pHttpFile != NULL)
		{
			delete m_pHttpFile;
			m_pHttpFile=NULL;
		}
		if (m_pConnection != NULL)
		{
			delete m_pConnection;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -