int CHQBmpCtrl::TryUrl(CString strAddress)
{
/**1
char chHeader[128];
char chData[128];
char tChars[1024];
memset(tChars,0,128);
CInternetSession session;
CHttpConnection* pConnection=NULL;
CHttpFile* pFile=NULL;
CFile newFile;
newFile.Open("d:\\fileName",CFile::modeCreate | CFile::modeWrite);
strcpy(chHeader,"Content-Type: application/x-www-form-urlencoded");
strcpy(chData,"Action=GetOrder&OrderNo=188D1&Sign=xa3s");
pConnection = session.GetHttpConnection(", "", "");
if(pConnection == NULL)
{
sprintf(tChars, "GetHttpConnect failed, error = %d (0x%x)/n", GetLastError(), GetLastError());
newFile.Write(tChars,strlen(tChars));
newFile.Flush();
newFile.Close();
}
newFile.Write("123",3);
pFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST,"/app");
if(pFile == NULL)
{
sprintf(tChars, "OpenRequest failed, error = %d (0x%x)/n", GetLastError(), GetLastError());
newFile.Write(tChars,strlen(tChars));
newFile.Flush();
newFile.Close();
}
newFile.Write("abc",3);
try
{
pFile->SendRequest(chHeader,(DWORD)strlen(chHeader),(LPVOID *)chData,strlen(chData));
}
catch(CInternetException* pEx)
{
newFile.Write("hhh\n",4);
sprintf(tChars, "OpenRequest failed, error = %d (0x%x)/n", GetLastError(), GetLastError());
newFile.Write(tChars,strlen(tChars));
newFile.Flush();
newFile.Close();
}
newFile.Write("ddd",3);
pFile->Read(tChars,128);
newFile.Write("ttt",3);
newFile.Write(tChars,strlen(tChars));
session.Close();
newFile.Close();
**/
/*2
CFile newFile;
CString fileName=strAddress.Mid(strAddress.ReverseFind('/')+1);
newFile.Open("d:\\fileName",CFile::modeCreate | CFile::modeWrite);
CInternetSession session;
CInternetFile* file=NULL;
try
{
file=(CInternetFile*)session.OpenURL(strAddress);
}
catch(CInternetException* pEx)
{
file=NULL;
pEx->Delete();
}
if(file)
{
int number=0;
BYTE *buf;
buf=new BYTE[1024];
do
{
number=file->Read(buf,1024);
newFile.Write(buf,number);
}
while(number>0);
delete []buf;
}
file->Close();
newFile.Close();
*/
/*3*/
CFile m_File; // 将返回数据写入文件
char szAccept[] = "Accept: */*";
CString szFormData ;// = "Action=GetOrder&OrderNo=188D1&Sign=xa3s";
char buf[128];
HINTERNET hSession;
HINTERNET hConnect;
HINTERNET hRequest;
BOOL bReturn = FALSE;
// 之前使用Socket,现在使用Wininet相关API建立链接
hSession = InternetOpen("HQBmp.ocx",INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
// hSession = InternetOpen("HQBmp.ocx",INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,INTERNET_FLAG_ASYNC );
if(!hSession)
return 34;
// hConnect = InternetConnect(hSession,");
hConnect = InternetConnect(hSession,");
// hConnect = InternetConnect(hSession,");
if(!hConnect)
return 35;
hRequest = HttpOpenRequest(hConnect,"POST","/pospay/getorder/","HTTP/1.1"," ",(LPCSTR *)&szAccept, INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE ,2);
if(!hRequest)
return 36;
// 提交数据表单
LPVOID pBuf = (LPVOID)szFormData.GetBuffer(szFormData.GetLength());
bReturn = HttpSendRequest(hRequest,"Content-Type: application/x-www-form-urlencoded\r\n",strlen("Content-Type: application/x-www-form-urlencoded\r\n"),pBuf,szFormData.GetLength());
if(!bReturn)
{
sprintf(buf, "HttpSendRequest failed, error = %d (0x%x)/n", GetLastError(), GetLastError());
m_File.Open("d:\\fileName1",CFile::modeCreate | CFile::modeWrite);
m_File.Write(buf,strlen(buf));
m_File.Flush();
m_File.Close();
InternetCloseHandle(hSession);
InternetCloseHandle(hConnect);
InternetCloseHandle(hRequest);
return 37;
}
char szRecvBuf[1024]; // 接受数据缓冲区
DWORD dwNumberOfBytesRead; // 服务器返回大小
m_File.Open("d:\\fileName1",CFile::modeCreate | CFile::modeWrite);
memset(szRecvBuf,0,1024);
do
{
// 开始读取数据
bReturn = InternetReadFile(hRequest,szRecvBuf,1023,&dwNumberOfBytesRead);
if(!bReturn)
return 38;
// 统计接受数据的大小
szRecvBuf[dwNumberOfBytesRead] = '\0';
// 将缓冲区写入文件
m_File.Write(szRecvBuf,strlen(szRecvBuf));
m_File.Flush();
memset(szRecvBuf, 0, 1024);
} while(bReturn && dwNumberOfBytesRead !=0);
m_File.Close();
InternetCloseHandle(hSession);
InternetCloseHandle(hConnect);
InternetCloseHandle(hRequest);
return 333;
}