我用以下页面,通过IE,在输入框输入"lucky", 可以从百度得到正确的搜索结果页面:
<html><head><title>百度——全球最大中文搜索引擎</title>
</head>
<body><br>
<form action=http://www1.baidu.com/baidu>
<input type=hidden name=cl value=3>
<input type=hidden name=tn value=baidu>
<input type=text name=word class=ff size=35>
<input type=submit value=百度搜索 name="submit">
</form>
</body></html>当我用VC做的客户端程序,模拟浏览器往百度的服务器post 表单的信息时,返回的是错误的信息.
请大家帮我看看哪里写得不对,非常感谢!
VC++源程序如下:void CGetHTTPDlg::OnButton1()
{
CString strServerName = _T("www1.baidu.com");
CString strFormAction = _T("baidu");
CString strHeaders =
_T("Content-Type: application/x-www-form-urlencoded");
CString strFormData = _T("cl=3&tn=baidu&word=lucky&submit=百度搜索");
CInternetSession session;
CHttpConnection* pConnection =
session.GetHttpConnection(strServerName);
CHttpFile* pFile =
pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST,strFormAction);
BOOL result = pFile->SendRequest(strHeaders,
(LPVOID)(LPCTSTR)strFormData, strFormData.GetLength()); CString smsg;
CStdioFile csfileReturnFile;
csfileReturnFile.Open("returnRequest.html", CFile::modeCreate | CFile::modeWrite | CFile::typeText))
while(pFile->ReadString(smsg)) //我把返回结果写到文件:returnRequest.html
{
csfileReturnFile.WriteString(smsg);
csfileReturnFile.WriteString("\n");
} csfileReturnFile.Close();
delete pFile;
delete pConnection;
session.Close();}
运行后,得到的returnRequest.html 文件内容是:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>400 Bad Request</TITLE>
</HEAD><BODY>
<H1>Bad Request</H1>
Your browser sent a request that this server could not understand.<P>
</BODY></HTML>
<html><head><title>百度——全球最大中文搜索引擎</title>
</head>
<body><br>
<form action=http://www1.baidu.com/baidu>
<input type=hidden name=cl value=3>
<input type=hidden name=tn value=baidu>
<input type=text name=word class=ff size=35>
<input type=submit value=百度搜索 name="submit">
</form>
</body></html>当我用VC做的客户端程序,模拟浏览器往百度的服务器post 表单的信息时,返回的是错误的信息.
请大家帮我看看哪里写得不对,非常感谢!
VC++源程序如下:void CGetHTTPDlg::OnButton1()
{
CString strServerName = _T("www1.baidu.com");
CString strFormAction = _T("baidu");
CString strHeaders =
_T("Content-Type: application/x-www-form-urlencoded");
CString strFormData = _T("cl=3&tn=baidu&word=lucky&submit=百度搜索");
CInternetSession session;
CHttpConnection* pConnection =
session.GetHttpConnection(strServerName);
CHttpFile* pFile =
pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST,strFormAction);
BOOL result = pFile->SendRequest(strHeaders,
(LPVOID)(LPCTSTR)strFormData, strFormData.GetLength()); CString smsg;
CStdioFile csfileReturnFile;
csfileReturnFile.Open("returnRequest.html", CFile::modeCreate | CFile::modeWrite | CFile::typeText))
while(pFile->ReadString(smsg)) //我把返回结果写到文件:returnRequest.html
{
csfileReturnFile.WriteString(smsg);
csfileReturnFile.WriteString("\n");
} csfileReturnFile.Close();
delete pFile;
delete pConnection;
session.Close();}
运行后,得到的returnRequest.html 文件内容是:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>400 Bad Request</TITLE>
</HEAD><BODY>
<H1>Bad Request</H1>
Your browser sent a request that this server could not understand.<P>
</BODY></HTML>
这个表单发查询的内容。
<html><head><title>百度——全球最大中文搜索引擎</title>
</head>
<body><br>
<form action=http://www1.baidu.com/baidu>
<input type=hidden name=cl value=3>
<input type=hidden name=tn value=baidu>
<input type=text name=word class=ff size=35>
<input type=submit value=百度搜索 name="submit">
</form>
</body></html>
CString strFormAction = _T("baidu?tn=baidu&ct=0&ie=gb2312&bs=CRM&sr=&z=&cl=3&f=8&word=%B0%D9%B6%C8%CB%D1%CB%F7");
CInternetSession session;
CHttpConnection* pConnection =
session.GetHttpConnection(strServerName);
CHttpFile* pFile =
pConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET,strFormAction);
BOOL result = pFile->SendRequest(NULL,NULL,0);
http://support.microsoft.com/default.aspx?kbid=165298#appliesto
是我忽视了你说的情况,一厢情愿的以为用POST 就一定能发表单了.