HTTP Post方法应用的例子

 
HTTP Post 方法应用的例子: (eddy)
首先:MFC例子:
void  post()  
 {  
      CInternetSession   session("My Session");  
      CHttpConnection*   pServer = NULL;  
      CHttpFile*   pFile =  NULL;  
      CString   ServerName   =   "webmail.21cn.com";   //
服务器地址
      INTERNET_PORT   nPort   =   80;                  //服务器端口号
      DWORD   retcode;  
      char   outBuff[300]   =   "LoginName=aaa&passwd=xxx&DomainName=21cn.com";        // I have   test   this   with   my   loginname   and   password  
      try  
      {  
         pServer =  session.GetHttpConnection(ServerName,nPort);  
 pFile=pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,"/N                uLL/NULL/NULL/NULL/NULL/SignIn.gen",NULL,1,NULL,NULL,INTERNET_FLAG_EXISTING_CONNECT);  
    pFile->AddRequestHeaders("Content-Type:application/x-www-form-urlencoded");  
    pFile->AddRequestHeaders("Accept:   */*");  
    pFile->SendRequest(NULL,0,outBuff,strlen(outBuff)+1);  
    pFile->QueryInfoStatusCode(retcode);  
 // you  can  read   from   the   file   after   this......I've   just   left   
   for(int   i=0;i<10;i++)//read   ten   line   of   returned   HTML,you   need                                                                                  
edit   this   to   use   the   result  
     {  
       CString   smsg;  
       pFile->ReadString(smsg);  
       AfxMessageBox(smsg);  
     }  
       DWORD  dwlen=pFile->GetLength();  
       char   buf[1024];  
       DWORD  dwread=pFile->Read(buf,1024);  
    }  
       catch   (CInternetException   *   e)
{
};  
      delete   pFile;  
       delete   pServer;  
       session.Close();  
}
 
socket 编程实现方法
 
///////////////////////////////////////////////////////////////////////////  
  //SDK   post  
  ///////////////////////////////////////////////////////////////////////////  
  #include   "stdafx.h"   // 该文件的作用见后面
  #include   "winsock.h"  
  #pragma   comment(lib,"ws2_32.lib")
//#pragma 时语言符号字符串,用来设置一些编译参数,这样就避免了命令操作,相当与一个//开关,用来开启某种服务的,如上例表示链接ws2_32.lib库 
  #define   winsock_version   0x0101  
  void   main()  
  {  
  //I  create C:/Inetpub/wwwroot/test/test.asp,start the  web service  
      //start   my   program,   the   result   is   OK.  
     //If   it   works,it   is   written   by   masterz,otherwise   I   don't       //   know   who   write   it. 
     SOCKADDR_IN   saServer;  
     LPHOSTENT   lphostent;   //lphostent
名称解析函数返回时的结构
     WSADATA   wsadata;  
     SOCKET   hsocket;  
     int   nRet;  
     const   char*   host_name="127.0.0.1";  
     char*   req="POST  /test/test.asp  HTTP/1.0/r/n"  
    "From:   local/r/n"  
    "User-Agent:   post_test/1.0/r/n"  
    "Content-Type:   application/x-www-form-urlencoded/r/n"  
   "Content-Length:   20/r/n/r/n"  
    "type=12345&name=aaaa";  
     if(WSAStartup(winsock_version,&wsadata))  
          printf("can't   initial   socket");  
          lphostent=gethostbyname(host_name);  
     if(lphostent==NULL)  
          printf("lphostent   is   null");  
     hsocket   =   socket(AF_INET,   SOCK_STREAM,   IPPROTO_TCP);  
         saServer.sin_family   =   AF_INET;  
    //   Use   def.   now,   need   to   handle   general   case  
        saServer.sin_port = htons(80);  
        saServer.sin_addr = *((LPIN_ADDR)*lphostent->h_addr_list); //
什么意思 ?
         nRet = connect(hsocket,(LPSOCKADDR)&saServer,sizeof(SOCKADDR_IN));  
     if(nRet == SOCKET_ERROR)     //
为了程序的流畅性,一般来说先写错误的情况
    {  
         printf("can't   connect");  
        closesocket(hsocket);  
       return;  
     }  
  else  
    printf("connected   with   %s/n",host_name);  
    nRet   =   send(hsocket,   req,   strlen(req),   0);  
  if(nRet   ==   SOCKET_ERROR)  
  {  
    printf("send() failed");  
    closesocket(hsocket);  
   
  }  
  else  
    printf("send()   OK/n");  
    char dest[1000];  
    nRet=1;          
    while(nRet>0)   /
/ 此处的赋值意欲何为 ?
  {  
     nRet=recv(hsocket,(LPSTR)dest,sizeof(dest),0);  
// LPSTR 被定义成是一个指向以 NULL(‘/0’) 结尾的 8 ANSI 字符数组指针 .
// LPWSTR 一指向以 NULL 结尾的 16 位双字节字符数组指针
if(nRet>0)  
    dest[nRet]=0;  
else  
    dest[0]=0;  
    printf("/nReceived   bytes:%d/n",nRet);  
    printf("Result:/n%s",dest);  
 }  
 }  
 

你可能感兴趣的:(session,socket,服务器,null,delete,internet)