前天下午终于把网易社区的发帖表单搞定了,一个按钮就能把一个帖子发通版。就是用winInet函数。
下面的这个函数就是向http服务器提交表单。但是九城和学生网怎么就不行呢,函数自动保存最后一个访问的页面到本地,老是说我没有权限,或者是游客。其实我的程序确实登录社区了,因为预览发帖能看到自己的用户名。为什么就不让我发呢?究竟winInet是不是能够完全的模仿用户自动登录等一切权限呢?服务器会不会采取措施防止winInet登录呢?
/*****************************************************************
* 函数介绍: 执行HTTP的Post或Get方法
* 输入参数: TCHAR* hdrs       - HTTP头
            TCHAR* accept     - Accept类型
            TCHAR* Method     - POST 或 GET
            TCHAR* frmdata    - 要提交的数据
            TCHAR* ServerName - 服务器地址
            TCHAR* FormAction - 数据提交到的网页名称
* 输出参数: 无
* 返 回 值: int               - 返回操作状态(见SendSMS)
*****************************************************************/
int doHTTP(TCHAR* hdrs, TCHAR* accept, TCHAR* Method, TCHAR* frmdata, TCHAR* ServerName, TCHAR* FormAction)
{
   // 创建Internet
   HINTERNET hSession = InternetOpen("MyAgent", 
                                     INTERNET_OPEN_TYPE_PRECONFIG, 
                                     NULL, 
                                     NULL,
                                     0);
   if (!hSession)
   {
       return 5;
   }
   // 连接服务器
   HINTERNET hConnect = InternetConnect(hSession, 
                                        ServerName, 
                                        INTERNET_DEFAULT_HTTP_PORT, 
                                        NULL, 
                                        NULL, 
                                        INTERNET_SERVICE_HTTP, 
                                        0, 
                                        1);
   if (!hConnect)
   {
   printf("Connect Fail!");
       return 2;
   }
   // 创建一个请求
   HINTERNET hRequest = HttpOpenRequest(hConnect,
                                        Method,
                                        FormAction,
                                        HTTP_VERSION,
                                        NULL, 
                                        (const char**)&accept,
                                        0, 
                                        1);
   if (!hRequest)
   {
   printf("Request Fail!");
       return 2;
   }
   // 发送请求
   BOOL bSendRequest = HttpSendRequest(hRequest,
                                       hdrs,
                                       strlen(hdrs),
                                       frmdata,
                                       strlen(frmdata));
   if (!bSendRequest)
   {
   printf("SendRequest Fail!");
       return 2;
   }////////////////////////调试用/////////////////
#ifdef _DEBUG
   int bDoLoop = 1;
   LPTSTR szReadBuffer;
   DWORD lNumberOfBytesRead;
   FILE* f1;
   szReadBuffer = (LPTSTR) malloc(500);
   ZeroMemory(szReadBuffer, 500);
   if ((f1=fopen("c:\\test.htm", "w"))!=NULL)
   {
       while(lNumberOfBytesRead>0)
       {
          bDoLoop = InternetReadFile(hRequest, szReadBuffer, 500, &lNumberOfBytesRead);
 
          fwrite(szReadBuffer, sizeof(char), lNumberOfBytesRead, f1); 
  printf("OK");
           
       }
   }
   else
   printf("File open fail!");
   fclose(f1);
   free(szReadBuffer);
#endif
//////////////////////////////////////////////////   // 清除句柄
   if (hRequest)
       InternetCloseHandle(hRequest);
   if (hConnect)
       InternetCloseHandle(hConnect);
   if (hSession)
       InternetCloseHandle(hSession);
   
   return 0;
}
下面是发送函数:int SendSMS(char* lpGateway,
  char* lpUserName, 
  char* lpPassword
   )
{
    int Result;
    static TCHAR hdrs[] = "Content-Type: application/x-www-form-urlencoded";    static TCHAR accept[] = "Accept: */*";
    static TCHAR frmdata[1024];    // 登陆姓名,密码等不允许为空
    if ((strlen(lpGateway)<=0)||(strlen(lpUserName)<=0)||
          (strlen(lpPassword)<=0))
        return 4;    // 选择网易网关发送
    if (strcmp(lpGateway, "163.com")==0)
    {
        // 登录发送系统
        sprintf(frmdata, "username=%s&password=%s", lpUserName,lpPassword);
        Result = doHTTP(hdrs, accept, "POST", frmdata, "reg4.163.com", "/in.jsp");
        sprintf(frmdata, "b=shijiazhuang&sign=0&back=0&subject=整理了好久! &body=<a  href=\"http://126p.126.com\">请进</a>");
       
   Result = doHTTP(hdrs, accept, "POST",frmdata, "fe4.bj.163.com", "/cgi/post");

        return Result;
    }  

  

   if (strcmp(lpGateway, "6to23.com")==0)
{
 sprintf(frmdata, "memuser=%s&mempass=%s", lpUserName,lpPassword);

Result = doHTTP(hdrs, accept, "POST", frmdata, "city.6to23.com", "/login/login.asp");

 sprintf(frmdata, "tinamed=哈哈哈&con=你好&t=365&Template=1&Filedata1=\"\"&status=1&corpus=y&rti=\"\"&reeeeeeeeeeeeee=dd");
 
 Result = doHTTP(hdrs, accept, "POST", frmdata, "city.6to23.com/sys/newarticle", "/review.asp");

 Result = doHTTP(hdrs, accept, "POST", frmdata, "city.6to23.com/sys/newarticle", "/tre.asp"); }}
主函数:
void main()
{
SendSMS("163.com","feicheng97","123456"); SendSMS("6to23.com","feicheng97","123456");
}网易的发出去了,学生网的不行,没道理的呀,review.asp都能提交,tre.asp就出问题了!高手请帮我查一下学生网表单,感谢万分!学生网:http://6to23.com