我用以下页面,通过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>
用snifferpro抓一下用IE浏览器搜索百度的包看一下,再分析一下百度的表单Top
2 楼haluo(哈罗)回复于 2003-12-12 02:39:36 得分 0
百度的表单就是我开始贴出来的那段,我删掉一些无关的东西而已。可以用
这个表单发查询的内容。
<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>Top
3 楼saucer(思归)回复于 2003-12-12 06:50:51 得分 90
they are using GET, not POST, try
CString strServerName = _T("www1.baidu.com");
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);
Top
4 楼haluo(哈罗)回复于 2003-12-12 11:07:45 得分 0
To saucer:
但上面那段发表单的VC代码我是从microsoft的网站抄下来的,应该不会有错啊?
http://support.microsoft.com/default.aspx?kbid=165298#appliestoTop
5 楼saucer(思归)回复于 2003-12-12 11:14:30 得分 0
你的编码编译是没错,但百度用的是GET,没用POST,你说应该怎么办?建议你研究一下HTTP协议Top
6 楼haluo(哈罗)回复于 2003-12-12 12:07:25 得分 0
非常感谢你,saucer, 问题解决了。
是我忽视了你说的情况,一厢情愿的以为用POST 就一定能发表单了.