我这个程序实现的是自动发送人人网站内信的功能,我模拟了post请求,但是怎么搞对方都收不到站内信,我都搞了好几个星期了,郁闷死了,请各位高手给我看看啊。下面有关键部分源码。各位高手,救救我吧!
/******************************************************************************************
*函数名称: SendMessage
*函数作用:模拟renren上的sendmessage
*函数参数:friend_id  发送的对方的好友id号  subject消息标题  status 消息内容
*by guobin
*******************************************************************************************/
bool RenrenSendMessage(char* friend_id, char* subject, char* status)
{
//将这几个参数初始化为CString
CString _subject(subject);
CString _status(status);
CString _friend_id(friend_id);  //庆

CString _my_id("228308981"); //函数内部
CString renren_header ;//请求头
// CHttpConnection* pServer;
// CHttpFile* pFile ;
CString tem_lineBuf; /*http请求的对象和发送的数据*/
CString strObject; 
CString strFormData; /*Host网址*/
CString HostURL="http://msg.renren.com"; 
CString strRetMsg; /*服务器和端口*/
CString strServer;
INTERNET_PORT nPort;  if(!AfxParseURL(HostURL,g_dwServiceType,strServer,strObject,nPort)|| 
g_dwServiceType != INTERNET_SERVICE_HTTP) 
{/*服务器网址验证失败*/
return false; 
}
/////////////////////////////////////////向这个好友 send message////////////////////////////////////////
//请求头
/*string my_boundary = get_boundary();
const char* c_char =my_boundary.c_str();*/
CString boundary = "----WebKitFormBoundaryZVqWse5n5jI83Nmk";
//CString boundary(c_char); printf("the boundary :%s\n", (LPVOID)(LPCTSTR)boundary); renren_header.Empty();
renren_header += _T("Host: msg.renren.com\r\n") ; renren_header += _T("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n") ;
renren_header += _T("Accept-Language: zh-cn\r\n") ;
//renren_header += _T("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.19 Safari/534.13\r\n") ;
renren_header += _T("Accept-Encoding: gzip,deflate,sdch\r\n");

renren_header += _T("Accept-Charset: gbk,utf-8;q=0.7,*;q=0.3\r\n");
//renren_header += _T("Origin: http://msg.renren.com\r\n");
renren_header += _T("Connection: Keep-Alive\r\n");

renren_header += (_T("Content-Type: multipart/form-data, boundary=") + boundary +_T("\r\n"));
//renren_header += _T("Cache-Control: max-age=0\r\n");
//renren_header += _T("Referer: http://msg.renren.com/message/compose.do?ref=t\r\n");

char *str = ("张庆");
//////转换字符串

WCHAR *strSrc;
TCHAR *szRes; //获得临时变量的大小
int i = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
strSrc = new WCHAR[i+1];
MultiByteToWideChar(CP_ACP, 0, str, -1, strSrc, i); //获得临时变量的大小
i = WideCharToMultiByte(CP_UTF8, 0, strSrc, -1, NULL, 0, NULL, NULL);
szRes = new TCHAR[i+1];
int j=WideCharToMultiByte(CP_UTF8, 0, strSrc, -1, szRes, i, NULL, NULL); CString _friend_name = szRes;
delete []strSrc;
delete []szRes; //types
strFormData.Empty();

strFormData += (_T("--") + boundary + _T("\r\n"));  //first boundary
strFormData += (_T("Content-Disposition: form-data; name=\"ids[]\"\r\n\r\n") + _friend_id); 
#if 1
//names
strFormData += _T("\r\n--")+  boundary +_T("\r\n");  // boundary
strFormData += (_T("Content-Disposition: form-data; name=\"names[]\"\r\n\r\n") + _friend_name);  //ids
strFormData += _T("\r\n--")+  boundary +_T("\r\n");  // boundary
strFormData += (_T("Content-Disposition: form-data; name=\"types[]\"\r\n\r\n0"));
//emails
strFormData += _T("\r\n--") + boundary +_T("\r\n");  // boundary
strFormData += (_T("Content-Disposition: form-data; name=\"emails[]\"\r\n\r\n")); 
#endif
//etypes
strFormData += _T("\r\n--") + boundary +_T("\r\n");  // boundary
strFormData += (_T("Content-Disposition: form-data; name=\"etypes[]\"\r\n\r\n0") ); 
//subject
strFormData += _T("\r\n--") + boundary +_T("\r\n");  //first boundary
strFormData += (_T("Content-Disposition: form-data; name=\"subject\"\r\n\r\n") + _subject); //subject
//message
strFormData += _T("\r\n--") + boundary +_T("\r\n");  //boundary
strFormData += (_T("Content-Disposition: form-data; name=\"message\"\r\n\r\n") + _status); 
#if 1
//biz
strFormData += _T("\r\n--") + boundary +_T("\r\n");  //boundary
strFormData += (_T("Content-Disposition: form-data; name=\"biz\"\r\n\r\n") + g_biz); 
#endif
//requestToken  get_check截取到的内容是这个token
strFormData += _T("\r\n--") + boundary +_T("\r\n");  //boundary
strFormData += (_T("Content-Disposition: form-data; name=\"requestToken\"\r\n\r\n") + g_get_check );  printf("the requestToken:%s\n", (LPVOID)(LPCTSTR)g_get_check);

strFormData +=_T("\r\n--") + boundary +_T("--\r\n");  //boundary CString strURL = "http://msg.renren.com";
if(!AfxParseURL(strURL,g_dwServiceType,strServer,strObject,nPort)||g_dwServiceType != INTERNET_SERVICE_HTTP) 
{/*服务器网址验证失败*/
// *errorLen = strlen(ServerConnectError);
// if(*errorLen>SWF_MAX_ERRORLENGTH)
// {/*如果过长则就截断失败码信息*/
// *errorLen = SWF_MAX_ERRORLENGTH;
// }
// memcpy(errorInfo,ServerConnectError,*errorLen);
    return false; 


#if 1
try
{
//g_pServer = g_Session_HG.GetHttpConnection(strServer,nPort); 
strObject=_T("/message/send.do");
//strObject += _T("&origURL=http://msg.renren.com/message/read.do?id=489883372&f=1&n=new");
g_pFile = g_pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST ,strObject);
//g_pFile = g_pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST ,strObject);
g_pFile->SendRequest(renren_header,(LPVOID)(LPCTSTR)strFormData, strFormData.GetLength());
}
catch(/* CInternetException *e*/... )
{
/*e->GetErrorMessage((LPTSTR)errorInfo,SWF_MAX_ERRORLENGTH);
*errorLen = strlen((LPTSTR)errorInfo);
e->Delete();*/
return false;
}
#else
try
{
strObject=_T("/message/send.do");
//strObject +=_T("&origURL=http%3A%2F%2Fmsg.renren.com%2Fmessage%2Fcompose.do%3Fref%3Dt");
pServer = g_Session_HG.GetHttpConnection(strServer,nPort); 
g_pFile = g_pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST ,strObject);

//while(1){
g_pFile->SendRequest(renren_header,(LPVOID)(LPCTSTR)strFormData, strFormData.GetLength());
//Sleep(1000);
//}
}//
catch(CInternetException *e)
{
char error[127];
e->GetErrorMessage(error,127);
return false;
}
         return true;}