| 发表于:2007-03-12 18:37:48 楼主 |
我实现一个向http1.1服务器上传文件的功能,http1.1服务器的web地址是固定的,我客户端会每次最多同时上传2个文件!因为http发送大文件会超时,所以文件采用分块传输的方法,每一块的大小是1m。 我查看了msdn的例子后写了如下函数 ,发送部分采用了sendrequestex()和endrequest函数(),在线程入口函数中的上传部分代码如下: ***注:因为是解决endrequest的问题,所以代码只贴上传这一关键部分的代码, 其中一些变量不作说过多说明 chttpconnection * phc = null; chttpfile * phf = null; cinternetsession cis; cis.setoption(internet_option_connect_timeout, 9000); cis.setoption(internet_option_connect_retries, 3); //重试次数 phc = cis.gethttpconnection(strserver, wport,temp-> user_name,temp-> user_password); //带“用户名”“密码”的http连接申请 while (send_now> 0) { int send_this=send_now> 1048576?1048576:send_now;//判断每一次循环需要发送的数据大小 try { m_file.seek((tailnumber)*1048576,cfile::begin); tailnumber++; //统计分块传输的次数 phf = phc-> openrequest(chttpconnection::http_verb_post, strobject); while(1) { int request_ret=phf-> sendrequestex(send_this); if (request_ret) { break; } } int read_now=send_this; while (read_now> 0) { int tag=read_now> 4096?4096:read_now; //上传文件的buffer选为4kb m_file.read(pfilebuff, tag); phf-> write(pfilebuff,tag); read_now=read_now-tag; } dword dwstatecode = 0; phf-> queryinfostatuscode(dwstatecode); if(dwstatecode == http_status_ok) bresult = true; phf-> endrequest(); //这一步总是抛出异常 } catch(cinternetexception * pex) { char sz[500] = " "; pex-> geterrormessage(sz, 500); int xxx=getlasterror(); cstring str; str.format( "internetexception occur!\r\n%s error code=%d ", sz,xxx); afxmessagebox(str); } catch(cfileexception& fe) { cstring str; str.format( "fileexception occur!\r\n%d ", fe.m_loserror); afxmessagebox(str); } catch(...) { dword dwerror = getlasterror(); cstring str; str.format( "unknow exception occur!\r\n%d ", dwerror); afxmessagebox(str); } } -------以下是我的测试结果-------------- 当我客户端只传一个9m的文件时,偶尔会在endrequest这一步抛出异常提示信息“the operation timed out!”,用getlasterror查看错误码为2;不过大部分分块传输完执行endrequest时都是正常的; 当我在客户端同时上传2个9m文件时,两个进程执行到endrequest时都会抛出异常信息“the operation timed out”,错误码还是2!但是虽然有异常信息,提示异常信息的那一块文件却可以发送正常,也就是说服务器那边可以接收到!但也存在部分块传输失败的情况。 ------------------------------------------------- 1。 请问此处关于endrequest异常的问题怎么解决? 2。 这个项目的http服务器那端的代码是我老板写的,是不是他那边没有处理我这个endrequest请求的模块,导致我这边超时呢? 我发现如果提示超时的话,这条http连接好像并没有立刻关掉,这样就导致我后来的http连接申请会因为windows系统对同一个http服务器的连接数限制而被阻塞掉,导致后面的数据都无法发送。 大家请指点以下,互相讨论一下! |
|
|
|
|