免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: send_linux
打印 上一主题 下一主题

[其他] 您了解HTTP么?工作中奇葩HTTP问题经验大征集!(获奖名单已公布-2014-7-9) [复制链接]

论坛徽章:
2
水瓶座
日期:2014-06-12 08:22:39IT运维版块每日发帖之星
日期:2015-08-05 06:20:00
1 [报告]
发表于 2014-06-05 20:53 |显示全部楼层
回复 1# send_linux


    前段时间想通过HTTP来上传文件,发生了一件奇葩事,,哈哈。。不多说直接上代码:
void COSBHTTPClient::UploadFile( TOSBDownloadParam& aParam, TDesC8& aContentType )

//aContentType  = “multipart/form-data; boundary=AaB03x”

{

        iClient->CancelTransaction();

        

        RFile rFile;

        RFs fs;

        if( KErrNone != fs.Connect() )

        {

                return;

        }



        //Open file where the stream text is

        TInt error = rFile.Open( fs, iDownloadParam->iFileName, EFileShareAny );//EFileRead );

        CleanupClosePushL( rFile );

        TInt size = -1;

        User::LeaveIfError( rFile.Size( size ) );        

        HBufC8* aXmlBuffer = HBufC8::NewLC( size );

        TPtr8 bufPtr( aXmlBuffer->Des() );

        error = rFile.Read( bufPtr );

        CleanupStack:op( aXmlBuffer ); // aXmlBuffer

        CleanupStack:opAndDestroy( &rFile ); // rFile

        fs.Close();

        _LIT8(KDataStart,"--AaB03x";

        _LIT8(KCrlf,"\r\n";

        _LIT8(KContent,"Content-Disposition: form-data; name='userfile'; filename='";

        _LIT8(KFileCompletion,"'";

         

        _LIT8(KContent2,"Content-Type: text/plain";

        _LIT8(KContent3,"Content-Transfer-Encoding: binary";

        _LIT8(KDataEnd,"--AaB03x--";

         

        HBufC8* iPostData = HBufC8::NewL( 650+ size );

         

        TPtr8 iPostDataPtr = iPostData->Des();

        iPostDataPtr.Zero();

         

        iPostDataPtr.Append(KCrlf);

        iPostDataPtr.Append(KDataStart);

        iPostDataPtr.Append(KCrlf);

        iPostDataPtr.Append(KContent);

//        iPostDataPtr.Append(aFname);

        iPostDataPtr.Append(KFileCompletion);

        iPostDataPtr.Append(KCrlf);

        iPostDataPtr.Append(KContent2);

        iPostDataPtr.Append(KCrlf);

        iPostDataPtr.Append(KContent3);

        iPostDataPtr.Append(KCrlf);

        iPostDataPtr.Append(KCrlf);

        iPostDataPtr.Append(bufPtr); //the file in binary

        iPostDataPtr.Append(KCrlf);

        iPostDataPtr.Append(KDataEnd);

        iPostDataPtr.Append(KCrlf);

        iClient->IssueHTTPPostL(URL, aContentType, iPostDataPtr);

        iObserver.ClientEvent( MOSBHTTPClientObserver::EEventConnecting );

}
复制代码void CClientEngine::IssueHTTPPostL( const TDesC8& aUri,

                const TDesC8& aContentType, const TDesC8& aBody )

{

        SetupConnectionL();



        // Parse string to URI

        TUriParser8 uri;

        TInt ret = uri.Parse( aUri );

        // Copy data to be posted into member variable; iPostData is used later in

        // methods inherited from MHTTPDataSupplier.

        delete iPostData;

        iPostData = aBody.AllocL();

        // Get request method string for HTTP POST

        RStringF method = iSession.StringPool().StringF(HTTP::EPOST, RHTTPSession::GetTable());



        // Open transaction with previous method and parsed uri. This class will

        // receive transaction events in MHFRunL and MHFRunError.

        iTransaction = iSession.OpenTransactionL( uri, *this, method );

        // Set headers for request; user agent, accepted content type and body's

        // content type.

        RHTTPHeaders hdr = iTransaction.Request().GetHeaderCollection();

        SetHeaderL( hdr, HTTP::EUserAgent, KUserAgent );

        SetHeaderL( hdr, HTTP::EAccept, KAccept );

        SetHeaderL( hdr, HTTP::EContentType, aContentType );

        // Set this class as an data supplier. Inherited MHTTPDataSupplier methods

        // are called when framework needs to send body data.

        MHTTPDataSupplier* dataSupplier = this;

        iTransaction.Request().SetBody( *dataSupplier );

        // Submit the transaction. After this the framework will give transaction

        // events via MHFRunL and MHFRunError.

        iTransaction.SubmitL();

}
复制代码void CClientEngine::MHFRunL( RHTTPTransaction aTransaction,

                const THTTPEvent& aEvent )

{



}
复制代码现在与服务器的连接可以成功,但是body总是传不上去,从log看MHFRunL的aEvent,走了下面3种状态
THTTPEvent::EGotResponseHeaders
THTTPEvent::EResponseComplete
THTTPEvent::ESucceeded
可是THTTPEvent::EGotResponseBodyData始终没有走到,自己总是以为上传失败,苦苦纠结了一个下午,,,最后才发现我是POST,不走THTTPEvent::EGotResponseBodyData,除非在POST给服务器的时候服务器还有返回信息才会走。。。。

论坛徽章:
2
水瓶座
日期:2014-06-12 08:22:39IT运维版块每日发帖之星
日期:2015-08-05 06:20:00
2 [报告]
发表于 2014-06-08 15:24 |显示全部楼层
好活动  支持支持。。。

论坛徽章:
2
水瓶座
日期:2014-06-12 08:22:39IT运维版块每日发帖之星
日期:2015-08-05 06:20:00
3 [报告]
发表于 2014-06-10 08:43 |显示全部楼层
回复 22# action08


    没错啊 我调试仿真过哦

论坛徽章:
2
水瓶座
日期:2014-06-12 08:22:39IT运维版块每日发帖之星
日期:2015-08-05 06:20:00
4 [报告]
发表于 2014-06-12 09:54 |显示全部楼层
支持支持
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP