- 论坛徽章:
- 2
|
回复 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给服务器的时候服务器还有返回信息才会走。。。。
|
|