首页
社区
课程
招聘
[求助]利用win32汇编写http协议的程序问题
发表于: 2006-10-24 05:05 5569

[求助]利用win32汇编写http协议的程序问题

2006-10-24 05:05
5569
在目前我所知道的关于连接互联网的函数中,就一个URLDownloadToFile来下载并且保存文件。

  大家都知道asp有get和post提交数据给服务器,在汇编中,如果要实现这些功能,到底有哪些函数呢?

  汇编应该有很多函数吧,在哪下载?我下了一个API电子书,谁知道是VB的。。。

请知道的朋友支招.....

[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!

收藏
免费 0
支持
分享
最新回复 (3)
雪    币: 211
活跃值: (40)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
2
API和语言无关   单独使用API操作HTTP比较困难MFC封装了很好的HTTP操作类
2006-10-24 08:28
0
雪    币: 796
活跃值: (370)
能力值: ( LV9,RANK:380 )
在线值:
发帖
回帖
粉丝
3
例如我要写个工具,利用get或post提交到服务器上去的话

需要哪些准备工作或者函数?
2006-10-24 17:52
0
雪    币: 297
活跃值: (10)
能力值: ( LV9,RANK:250 )
在线值:
发帖
回帖
粉丝
4
建议楼主看看RFC文档,实在不行可以装个袖探器简单分析下自己机器上 HTTP协议的格式.
然后在用winsock去进行数据的封装和发送.
关键是对协议的认识和利用.
这是我以前写的一个web服务端程序的核心http协议处理部分,主要就是处理客户端提交get或者post数据的过程.比如get 的时候要提取出对方要读取的文件名,然后读到内存,发送到对方.或者对方post的数据,读到内存,写入相关的文件.
这个过程是很有意思的:)完整的代码见http://redcoder.blog.sohu.com/10845511.html
DWORD WINAPI Recv_Send_Thread( DWORD* pindex )
{
char      recvmem[1024];
char      TimeFmr[] = "%d,%d/%d/%d %d:%d:%d GMT";
char   MimeType[]="%s/%s"; 
char   outtime[50];
char      Type[40];

DWORD      socketindex = *( (DWORD *)pindex ); 
int      errorcode = 0;
DWORD      sendbt=0;
char   headers[500];
char      hdrFmtNor[]=
"HTTP/1.0 200 OK\r\n"
"Server: KIKI's Web Server\r\n"
"Date: %s\r\n"
"Accept-Ranges: bytes\r\n"
"Content-Length: %d\r\n"
"Content-Type: %s\r\n\r\n";

char      hdrFmtDown[]=
"HTTP/1.0 206 Partial content\r\n"
"Server: KIKI's Web Server\r\n"
"Date: %s\r\n"
"Accept-Ranges: bytes\r\n"
"Content-Length: %d\r\n"
"Content-Type: %s\r\n\r\n";


memset( recvmem , 0 ,1024 );
errorcode = recv( TranSock[socketindex] , recvmem , 1024 , 0 );
if( errorcode == SOCKET_ERROR || errorcode == 0 )
     {printf( "Recv Error: %d\n" , WSAGetLastError() );
      //printf("----------------------------------------"
     //         "----------------------------------------");   
      closesocket( TranSock[socketindex] );
      TranSock[socketindex] = 0;
      return -1;
     } 

wsprintf( outtime , TimeFmr , currenttime.wDayOfWeek,
                                        currenttime.wDay,
                                            currenttime.wMonth ,
                                            currenttime.wYear ,
                                            currenttime.wHour ,
                                            currenttime.wMinute ,
                                            currenttime.wSecond);

printf( "%s\n" , recvmem );

HANDLE      fp;
char      sendfile[1400];
char      filename[50] = "index.htm";
char      tempname[10] = "index.htm";
DWORD      i = 0;
DWORD      dwRead = 0;
int      len;


memset( sendfile , 0 , 1400 );
strcpy( filename , tempname ); 

if(strlen(recvmem) < 5)
{ printf("这是一组恶意数据\n\n");
  closesocket(TranSock[socketindex]);
  TranSock[socketindex] = 0;
  return -1;
}
     
if( recvmem[5]!=32 )
     { memset(filename,0,50);
        for(i=5;recvmem[i]!=32;i++)
            filename[i-5]=recvmem[i];
     }


     
for( i=0 ; i<50 ; i++ )
if( (filename[i]==':') || (filename[i]=='*') || (filename[i]=='%') )
{printf( "Time:%s\n%s企图请求的页面文件:%s.\n\n" , outtime , inet_ntoa(client[socketindex].sin_addr) , filename );
printf("\n--------------------------------------"
        "------------------------------------------");
closesocket(TranSock[socketindex]);
TranSock[socketindex] = 0;
return -1;
}
i=0;
DWORD j=0 ;
DWORD start; 
char size[32];
memset(size,0,32);
while(i != 1024)
{if(recvmem[i] == '=')
     {i++; 
      while(recvmem[i]!='-')
           {size[j] = recvmem[i];
            i++;
            j++;
           }
     break;
     }
else
      i++;

}
if(size[0] == 0)
start = 0;
else
start = atol(size);


printf("range:%d\n",start);      

fp = CreateFile(filename,
                        GENERIC_READ,
                        FILE_SHARE_READ,
                        (LPSECURITY_ATTRIBUTES)NULL,
                        OPEN_EXISTING,
                        FILE_ATTRIBUTE_NORMAL,
                        NULL);
if( fp == INVALID_HANDLE_VALUE )
{printf( "%s open file: %s error!\n" , inet_ntoa( client[socketindex].sin_addr ) , filename );
printf( "--------------------------------------------------------------------------------" );
closesocket( TranSock[socketindex] );
TranSock[socketindex] = 0;
return -1;
}


i = GetFileSize(fp,NULL);
j = i / 1400;

len = strlen( filename );
if((filename[len-3]=='h'&&filename[len-2]=='t'&&filename[len-1]=='m')||\
     (filename[len-3]=='H'&&filename[len-2]=='T'&&filename[len-1]=='M')||\
     (filename[len-4]=='h'&&filename[len-3]=='t'&&filename[len-2]=='m'&&filename[len-1]=='l')||\
     (filename[len-4]=='H'&&filename[len-3]=='T'&&filename[len-2]=='M'&&filename[len-1]=='L')||\
  (filename[len-5]=='s'&&filename[len-4]=='h'&&filename[len-3]=='t'&&filename[len-2]=='m'&&filename[len-1]=='l')||\
     (filename[len-5]=='S'&&filename[len-4]=='H'&&filename[len-3]=='T'&&filename[len-2]=='M'&&filename[len-1]=='L'))
     wsprintf(Type,MimeType,"text","html");

else if(
     (filename[len-3]=='j'&&filename[len-2]=='p'&&filename[len-1]=='g')||\
     (filename[len-3]=='J'&&filename[len-2]=='P'&&filename[len-1]=='G')||\
     (filename[len-3]=='j'&&filename[len-2]=='p'&&filename[len-1]=='e')||\
     (filename[len-3]=='J'&&filename[len-2]=='P'&&filename[len-1]=='E')||\
     (filename[len-4]=='j'&&filename[len-3]=='p'&&filename[len-2]=='e'&&filename[len-1]=='g')||\
     (filename[len-4]=='J'&&filename[len-3]=='P'&&filename[len-2]=='E'&&filename[len-1]=='G'))
     wsprintf(Type,MimeType,"image","jpeg"); 

else if(
     (filename[len-3]=='g'&&filename[len-2]=='i'&&filename[len-1]=='f')||\
     (filename[len-3]=='G'&&filename[len-2]=='I'&&filename[len-1]=='F'))
     wsprintf(Type,MimeType,"image","gif"); 

else if(
     (filename[len-3]=='c'&&filename[len-2]=='s'&&filename[len-1]=='s')||\
     (filename[len-3]=='C'&&filename[len-2]=='S'&&filename[len-1]=='S'))
     wsprintf(Type,MimeType,"text","css");

else if(
     (filename[len-3]=='t'&&filename[len-2]=='x'&&filename[len-1]=='t')||\
     (filename[len-3]=='T'&&filename[len-2]=='X'&&filename[len-1]=='T'))
     wsprintf(Type,MimeType,"text","plain");

else if(
     (filename[len-3]=='p'&&filename[len-2]=='d'&&filename[len-1]=='f')||\
     (filename[len-3]=='P'&&filename[len-2]=='D'&&filename[len-1]=='F'))
     wsprintf(Type,MimeType,"application","pdf");

else if(
     (filename[len-3]=='s'&&filename[len-2]=='w'&&filename[len-1]=='f')||\
     (filename[len-3]=='S'&&filename[len-2]=='W'&&filename[len-1]=='F')||\
     (filename[len-3]=='c'&&filename[len-2]=='a'&&filename[len-1]=='b')||\
     (filename[len-3]=='C'&&filename[len-2]=='A'&&filename[len-1]=='B'))
     wsprintf(Type,MimeType,"application","x-shockwave-flash");

else if(
     (filename[len-3]=='d'&&filename[len-2]=='o'&&filename[len-1]=='c')||\
     (filename[len-3]=='D'&&filename[len-2]=='O'&&filename[len-1]=='C')||\
     (filename[len-3]=='d'&&filename[len-2]=='o'&&filename[len-1]=='t')||\
     (filename[len-3]=='D'&&filename[len-2]=='O'&&filename[len-1]=='T'))
     wsprintf(Type,MimeType,"application","msword");

else if(
     (filename[len-3]=='h'&&filename[len-2]=='l'&&filename[len-1]=='p')||\
     (filename[len-3]=='H'&&filename[len-2]=='L'&&filename[len-1]=='P')||\
     (filename[len-3]=='c'&&filename[len-2]=='h'&&filename[len-1]=='m')||\
     (filename[len-3]=='C'&&filename[len-2]=='H'&&filename[len-1]=='M'))
     wsprintf(Type,MimeType,"application","mshelp");

else if(
     (filename[len-3]=='x'&&filename[len-2]=='l'&&filename[len-1]=='s')||\
     (filename[len-3]=='X'&&filename[len-2]=='L'&&filename[len-1]=='S')||\
     (filename[len-3]=='x'&&filename[len-2]=='l'&&filename[len-1]=='a')||\
     (filename[len-3]=='X'&&filename[len-2]=='L'&&filename[len-1]=='A'))
     wsprintf(Type,MimeType,"application","msexcel");

else if(
     (filename[len-3]=='p'&&filename[len-2]=='p'&&filename[len-1]=='t')||\
     (filename[len-3]=='P'&&filename[len-2]=='P'&&filename[len-1]=='T')||\
     (filename[len-3]=='p'&&filename[len-2]=='p'&&filename[len-1]=='z')||\
     (filename[len-3]=='P'&&filename[len-2]=='P'&&filename[len-1]=='T')||\
     (filename[len-3]=='p'&&filename[len-2]=='p'&&filename[len-1]=='s')||\
  (filename[len-3]=='P'&&filename[len-2]=='P'&&filename[len-1]=='S')||\
     (filename[len-3]=='p'&&filename[len-2]=='o'&&filename[len-1]=='t')||\
     (filename[len-3]=='P'&&filename[len-2]=='O'&&filename[len-1]=='T'))
     wsprintf(Type,MimeType,"application","mspowerpoint");
     


else if(
     (filename[len-3]=='b'&&filename[len-2]=='i'&&filename[len-1]=='n')||\
     (filename[len-3]=='B'&&filename[len-2]=='I'&&filename[len-1]=='N')||\
     (filename[len-3]=='e'&&filename[len-2]=='x'&&filename[len-1]=='e')||\
     (filename[len-3]=='E'&&filename[len-2]=='X'&&filename[len-1]=='E')||\
     (filename[len-3]=='c'&&filename[len-2]=='o'&&filename[len-1]=='m')||\
     (filename[len-3]=='C'&&filename[len-2]=='O'&&filename[len-1]=='M')||\
     (filename[len-3]=='d'&&filename[len-2]=='l'&&filename[len-1]=='l')||\
     (filename[len-3]=='D'&&filename[len-2]=='L'&&filename[len-1]=='L')||\
     (filename[len-5]=='c'&&filename[len-4]=='l'&&filename[len-3]=='a'&&filename[len-2]=='s'&&filename[len-1]=='s')||\
     (filename[len-5]=='C'&&filename[len-4]=='L'&&filename[len-3]=='A'&&filename[len-2]=='S'&&filename[len-1]=='S'))
     wsprintf(Type,MimeType,"application","octet-stream");
     
else if(
     (filename[len-3]=='a'&&filename[len-2]=='v'&&filename[len-1]=='i')||\
     (filename[len-3]=='A'&&filename[len-2]=='V'&&filename[len-1]=='I'))
     wsprintf(Type,MimeType,"video","x-msvideo");      
     
     else
     wsprintf(Type,MimeType,"*","*");
  
     
     
if(start == 0)
wsprintf(headers, hdrFmtNor, (const char*)outtime, i ,Type);
else
wsprintf(headers, hdrFmtDown, (const char*)outtime, i-start ,Type);

printf( "Time:%s\n%s企图请求的页面文件:%s . 使用的套接字ID:Socket[%d]\n" , outtime , inet_ntoa(client[socketindex].sin_addr) , filename, socketindex );
sendbt = send( TranSock[socketindex] , headers , strlen(headers) , 0 );
if(-1 == SetFilePointer(fp,start,NULL,FILE_BEGIN))
{printf("SetFilePointer Error :%d\n",GetLastError());
      closesocket( TranSock[socketindex] );
           TranSock[socketindex] = 0;
           CloseHandle(fp);
           return -1;
}
do{
      BOOL fRead = ReadFile(fp,
                                      sendfile,
                                         1400,
                                         &dwRead,
                                         NULL);
      if(fRead == FALSE)
      {      printf("读取文件错误!文件名:%s\n" , filename);
           closesocket( TranSock[socketindex] );
           TranSock[socketindex] = 0;
           CloseHandle(fp);
           return -1;
      }
      else if(fRead && dwRead)
      {      sendbt = send( TranSock[socketindex] , sendfile , 1400 , 0 );
        if( sendbt == SOCKET_ERROR )
           {printf("Send Error:%d!\n",GetLastError());
            closesocket( TranSock[socketindex] );
            TranSock[socketindex] = 0;
            CloseHandle(fp);
            return -1;
           }
        memset(sendfile , 0 , 1400);
      }
      else
      { sendbt = send( TranSock[socketindex] , sendfile , j , 0 );
        if( sendbt == SOCKET_ERROR )
           {printf("Send Error:%d!\n",GetLastError());
            closesocket( TranSock[socketindex] );
            CloseHandle(fp);
            TranSock[socketindex] = 0;
            return -1;
           }
      }
}while(dwRead == 1400);



printf( "Socket[%d]数据传输完毕!\n\n" , socketindex );
CloseHandle(fp);
closesocket( TranSock[socketindex] );
TranSock[socketindex] = 0;
return 0;
}
2006-10-31 21:21
0
游客
登录 | 注册 方可回帖
返回
//