用mfc的http类写一个程序,模拟ie访问目标页面,将参数传过给页面去即可。如搜狐俄的是一个php页面
解决方案 »
- 一道sizeof的面试题
- 想在屏幕上画出条直线,编译通过,但是看不到效果,求高人指点~~~
- 请问局域网通信中怎样获取当前在线人数?
- opengl 拾取的问题。比如2个对象有重合,而我只想拾取其中1个对象。
- ADO中奇怪的问题!
- TCP createthread 开辟线程
- 期待牛人解决这个问题 ,百分赠送
- 菜鸟问题
- 有关BCMenu类应用的问题,诚恳指教!!完分感谢!!
- 急用!兄弟,推荐有关用VC++、http协议进行web网络编程的书籍!
- 我想作一个数据库的程序,库文件可能会有好几百兆,我该用那种格式的数据库文件?
- 我用程序生成了一个8K,16BIT,MONO的WAV文件,内容是频率为1K HZ的正弦波,请问有什么办法检测这个WAV文件真的是1K的正弦波呢?
例:HttpSend("www.csdn.net","test.asp?","id=233&name=heilong",TRUE);
最後一个参数IsAuto是我程序重要用到的,对大家来说没什么大用,大家可以去掉,主要是看看用mfc的Wininet类建立一个internet程序的步骤,这些msdn中都有,不过写这个函数可花了我一天的时间,没办法,偶以前没做过,现在共享以下,欢迎大家指教,我的email是[email protected]
/////////////////////////////////以下为函数源码
BOOL CDicosDlg::HttpSend(CString strSever, CString Object,CString strValue,BOOL bIsAuto)
{
//CString temp = Object + strValue;
//temp = strSever + temp;
//MessageBox(temp);
CWnd* butTran = GetDlgItem (IDC_BUTTON_TRANSCOM);
if (bIsAuto)//说明正在Ontimer中直接调用该函数,应该屏蔽掉其他的发送
{
butTran->EnableWindow (FALSE);
bIsTranscom = TRUE;
}
CHttpConnection* m_Connection = NULL;
CHttpFile* m_File = NULL;
CString m_Ret;//用来接收返回信息
CInternetSession m_Session(AfxGetAppName (),0,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
TRY
{
//DWORD dwValue = 0;
//m_Session.QueryOption (INTERNET_OPTION_CONNECT_RETRIES,dwValue);
m_Session.SetOption (INTERNET_OPTION_CONNECT_RETRIES,1);
m_Session.SetOption (INTERNET_OPTION_CONNECT_TIMEOUT,20000);
m_Session.SetOption (INTERNET_OPTION_RECEIVE_TIMEOUT,20000);
INTERNET_PORT nPort = 80;
CString strServer = strSever;
m_Connection = m_Session.GetHttpConnection(strServer,nPort,NULL,NULL );
if (m_Connection == NULL)
{
DisplayStat ("An error occurred connecting to the server");
Sleep (2000);
goto end;
}
LPCTSTR ppszAcceptTypes[2];
ppszAcceptTypes[0] = _T("*/*");
ppszAcceptTypes[1] = NULL;
CString strObject = Object;
strObject = Object + strValue;
m_File = m_Connection->OpenRequest(NULL, strObject, NULL,1,ppszAcceptTypes, NULL, INTERNET_FLAG_RELOAD ¦
INTERNET_FLAG_DONT_CACHE);
if (m_File == NULL)
{
DisplayStat ("打开服务器上的文件出错");
/*CStdioFile debugFile;
debugFile.Open ("debug.txt",CFile::modeWrite¦CFile::modeCreate¦CFile::modeNoTruncate¦CFile::shareDenyWrite¦CFile::typeText);
debugFile.SeekToEnd ();
CString strInf("打开服务器上的文件出错");
strInf = Object + strInf + "\n";
debugFile.Write (strInf.GetBuffer (strInf.GetLength ()),strInf.GetLength ());
strInf.ReleaseBuffer ();*/
goto end;
}
if (!m_File->SendRequest (NULL,0,NULL,0))
{
DisplayStat ("An error occurred connecting to the server");
Sleep (2000);
goto end;
}
TCHAR szStatusCode[32];
DWORD dwInfoSize = 32;
if (m_File->QueryInfo (HTTP_QUERY_STATUS_CODE,szStatusCode,&dwInfoSize))
{
long nStatusCode = _ttol(szStatusCode); //Handle any authentication errors
if (nStatusCode == HTTP_STATUS_REQUEST_TIMEOUT)
{
DisplayStat ("连接超时,请重试");
/*CStdioFile debugFile;
debugFile.Open ("debug.txt",CFile::modeWrite¦CFile::modeCreate¦CFile::modeNoTruncate¦CFile::shareDenyWrite¦CFile::typeText);
debugFile.SeekToEnd ();
CString strInf ("连接超时,请重试");
strInf = Object + strInf + "\n";
debugFile.Write (strInf.GetBuffer (strInf.GetLength ()),strInf.GetLength ());
strInf.ReleaseBuffer ();*/
goto end;
// We have to read all outstanding data on the Internet handle
// before we can resubmit request. Just discard the data.
}
else if (nStatusCode != HTTP_STATUS_OK)
{
DisplayStat("服务器端错误");
/*CStdioFile debugFile;
debugFile.Open ("debug.txt",CFile::modeWrite¦CFile::modeCreate¦CFile::modeNoTruncate¦CFile::shareDenyWrite¦CFile::typeText);
debugFile.SeekToEnd ();
CString strInf("服务器端错误");
strInf = Object + strInf + szStatusCode + "\n";
debugFile.Write (strInf.GetBuffer (strInf.GetLength ()),strInf.GetLength ());
strInf.ReleaseBuffer ();*/
goto end;
}
//状态正确,开始读取信息
}
else
{
DisplayStat("无法取得服务器返回信息");
/*CStdioFile debugFile;
debugFile.Open ("debug.txt",CFile::modeWrite¦CFile::modeCreate¦CFile::modeNoTruncate¦CFile::shareDenyWrite¦CFile::typeText);
debugFile.SeekToEnd ();
CString strInf("无法取得服务器返回信息");
strInf = Object + strInf + "\n";
debugFile.Write (strInf.GetBuffer (strInf.GetLength ()),strInf.GetLength ());
strInf.ReleaseBuffer ();*/
goto end;
}
char szReadBuf[1024];
DWORD dwBytesToRead = 1024;
DWORD m_ActualSize = 0;
do
{
ZeroMemory (szReadBuf,1024);
DWORD m_ActualSize = m_File->Read (szReadBuf,dwBytesToRead);
m_Ret = m_Ret + szReadBuf;
}while(!(m_ActualSize < dwBytesToRead));
TRACE("%s",m_Ret);
int iFirst = m_Ret.Find ("<返回值>",0);
CString strTemp = m_Ret.Right (m_Ret.GetLength () - iFirst - 8);
int iLast = strTemp.Find ("</返回值>",0);
strTemp = strTemp.Left (iLast);
if (strTemp == "操作成功")
{
DisplayStat("数据上传成功");
/*CStdioFile debugFile;
debugFile.Open ("debug.txt",CFile::modeWrite¦CFile::modeCreate¦CFile::modeNoTruncate¦CFile::shareDenyWrite¦CFile::typeText);
debugFile.SeekToEnd ();
CString strInf ("数据上传成功");
strInf = Object + strInf + "\n";
debugFile.Write (strInf.GetBuffer (strInf.GetLength ()),strInf.GetLength ());
strInf.ReleaseBuffer ();*/
if (m_File)
{
m_File->Close ();
delete m_File;
}
if (m_Connection)
{
m_Connection->Close ();
delete m_Connection;
}
m_Session.Close ();
if (bIsAuto)
{
butTran->EnableWindow ();
bIsTranscom = FALSE;
}
DisplayStat ("空闲");
return TRUE;
}
else
{
DisplayStat("数据上传失败,请稍候重试");
/*CStdioFile debugFile;
debugFile.Open ("debug.txt",CFile::modeWrite¦CFile::modeCreate¦CFile::modeNoTruncate¦CFile::shareDenyWrite¦CFile::typeText);
debugFile.SeekToEnd ();
CString strInf ("数据上传失败,请稍候重试");
strInf = Object + strInf + "\n";
debugFile.Write (strInf.GetBuffer (strInf.GetLength ()),strInf.GetLength ());
strInf.ReleaseBuffer ();*/
goto end;
}
//int i = m_Content.Replace ("\r\n","\n");
}
CATCH (CInternetException, e)
{
char errStr[100];
e->GetErrorMessage (errStr,100);
DisplayStat (errStr);
/*CStdioFile debugFile;
debugFile.Open ("debug.txt",CFile::modeWrite¦CFile::modeCreate¦CFile::modeNoTruncate¦CFile::shareDenyWrite¦CFile::typeText);
debugFile.SeekToEnd ();
CString strInf("InternetException");
strInf = strInf + errStr + "\n";
debugFile.Write (strInf.GetBuffer (strInf.GetLength ()),strInf.GetLength ());
strInf.ReleaseBuffer ();*/
if (m_File)
{
m_File->Close ();
delete m_File;
m_File = NULL;
}
if (m_Connection)
{
m_Connection->Close ();
delete m_Connection;
m_Connection = NULL;
}
m_Session.Close ();
if (bIsAuto)
{
butTran->EnableWindow ();
bIsTranscom = FALSE;
}
DisplayStat ("空闲");
return FALSE;
//return FALSE;
}
//CATCH_ALL (e)
//{
//}
END_CATCH
end:
if (m_File)
{
m_File->Close ();
delete m_File;
m_File = NULL;
}
if (m_Connection)
{
m_Connection->Close ();
delete m_Connection;
m_Connection = NULL;
}
m_Session.Close ();
if (bIsAuto)
{
butTran->EnableWindow ();
bIsTranscom = FALSE;
}
DisplayStat ("空闲");
return FALSE;
}
HttpSend("www.sohu.com","/message/freesend.php?","ToNum=13902231958&UsrName=heilong&UsrNum=130322666&UsrPwd=1556",TRUE);
其中,usrname如果为中文的话还要进行转换,转化为“%”加中文每个字节的ascii码.自己多试试,就是用mfc的http类模仿ie的动作嘛。
欢迎跟我联系!
QQ:509832 [email protected]