vc++-mfc-实现网站发布文章ping百度,加快文章收录
// http 链接变量 CHttpConnection * m_http; CInternetSession CIS; CString strPingBaidu; strPingBaidu = "ping.baidu.com"; CString strPingPath; strPingPath = "ping/RPC2"; pdlg->m_http = CIS.GetHttpConnection(strPingBaidu) ; CHttpFile * pHttpFile = NULL; /* CHttpFile* OpenRequest( LPCTSTR pstrVerb,//get or post .A pointer to a string containing the verb to use in the request. If NULL, "GET" is used. LPCTSTR pstrObjectName,//提交站点内地址 LPCTSTR pstrReferer = NULL,//请求参数字符串 DWORD_PTR dwContext = 1,//返回状态码 LPCTSTR* ppstrAcceptTypes = NULL,//返回的字符串 LPCTSTR pstrVersion = NULL,//A pointer to a string defining the HTTP version. If NULL, "HTTP/1.0" is used. DWORD dwFlags = INTERNET_FLAG_EXISTING_CONNECT ); */ pHttpFile = pdlg->m_http->OpenRequest(CHttpConnection::HTTP_VERB_POST, strPingPath,//"test/test.php", NULL, 1, NULL, NULL, INTERNET_FLAG_EXISTING_CONNECT); //发送header //pHttpFile->AddRequestHeaders("Content-Type:application/x-www-form-urlencoded"); //pHttpFile->AddRequestHeaders("Accept:*/*"); /* POST /ping/RPC2 HTTP/1.0 User-Agent: request Host: ping.baidu.com Content-Type: text/xml Content-Length: 511 */ pHttpFile->AddRequestHeaders("POST /ping/RPC2 HTTP/1.0"); pHttpFile->AddRequestHeaders("User-Agent: request"); pHttpFile->AddRequestHeaders("Host: ping.baidu.com"); pHttpFile->AddRequestHeaders("Content-Type: text/xml"); //发送请求 //这个是post的 CString strBlogName,strBlogIndexURL,strNewArticleURL,strRssURL; strBlogName = pdlg->m_CtrlRequestURLInfo.GetItemText(j,1); strBlogIndexURL = pdlg->m_CtrlRequestURLInfo.GetItemText(j,1); strNewArticleURL = strURL; strRssURL = ""; CString strPost; strPost = GetPingCString(strBlogName,strBlogIndexURL,strNewArticleURL,strRssURL); //strPost = pdlg->GetPingCString("d","d","d","d"); //post的数据 //strPost = "sdafdfsadf"; //post的数据 //pHttpFile->SendRequest(NULL,0,(LPVOID)(LPCTSTR)strPost,strPost.GetLength()); //if (pHttpFile->SendRequest(NULL,0,NULL,0)) if(pHttpFile->SendRequest(NULL,0,(LPVOID)(LPCTSTR)strPost,strPost.GetLength())) { //成功 DWORD retcode; pHttpFile->QueryInfoStatusCode(retcode); CString strReturn; if (retcode == 200) { //一行一行的读出来 CString strLine; while(pHttpFile->ReadString(strLine)) { strReturn = strReturn + strLine + "\n"; } pdlg->AddStatus(strReturn); //显示出结果 if (strReturn.Replace("0","ok")==1) { //能替换一个,说明返回的是0,也就是说提交成功了 pdlg->m_CtrlBaiduPing.SetItemText(pdlg->m_CtrlBaiduPing.GetItemCount()-1,2,"是"); pdlg->AddStatus("Ping百度成功"); } else if (strReturn.Replace("1","ok")==1) { pdlg->AddStatus("Ping百度失败"); } //pdlg->AddStatus(strReturn); //AfxMessageBox(strReturn); } } else { //失败 pdlg->AddStatus("send request 失败"); }
自己自定义的一个函数
CString GetPingCString(CString blogname, CString blogindexurl, CString newarticleurl, CString rssurl) { /* POST /ping/RPC2 HTTP/1.0 User-Agent: request Host: ping.baidu.com Content-Type: text/xml Content-Length: 511 weblogUpdates.extendedPing百度的空间http://hi.baidu.com/baidu/http://baidu.com/blog/example.html http://hi.baidu.com/baidu/rss */ CString strReturn; strReturn = strReturn = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"; strReturn = strReturn + "<methodCall><methodName>weblogUpdates.extendedPing</methodName><params><param><value><string>" + blogname + \ "</string></value></param><param><value><string>" + blogindexurl + "</string></value></param><param><value><string>" + newarticleurl +\ "</string></value></param><param><value><string>" + rssurl + "</string></value></param></params></methodCall>"; return strReturn; }