Hi,
I want to post an XML file to server.I am using HTTP client example. has anybody done file post with this? how can I send file as a body? what function need to be re implemented and how? If someone can elaborate it will be great help,Thx.
Hi,
I want to post an XML file to server.I am using HTTP client example. has anybody done file post with this? how can I send file as a body? what function need to be re implemented and how? If someone can elaborate it will be great help,Thx.
You might consider typing http post in the Search field of the Wiki.
I have gone through few related posts but couldn't find the exact information.
Here's my code.I'm not able to save the file on the server.I m using apache server.I have PHP script to support the upload.can someone tell me what's the problem?
//GetNextDataPart function
TBool CDMDatasyncPost::GetNextDataPart(TPtrC8& aDataPart)
{
iRetVal = EFalse;
if ( iIsDataReleased )
{
if ( iPostData )
{
TInt offset = KMaxSubmitSize;
if ( iStartPos + offset > iPostData->Des().Length() )
{
offset = iRemainingLength ;
}
// Copy the part buffer for posting
TPtrC8 startRip = iPostData->Des().Mid(iStartPos);
iPartPostBuffer.Copy((const unsigned char *)startRip.Ptr(),offset);
aDataPart.Set(iPartPostBuffer);
iStartPos += offset;
iRemainingLength -= offset;
if ( iRemainingLength == 0 )
{
iRetVal = ETrue;
delete iPostData;
iPostData= NULL;
}
}
}
else
{
aDataPart.Set(iPartPostBuffer);
}
return iRetVal;
}
----------------------------------------------------------------
//ReleaseData function
void CDMDatasyncPost::ReleaseData()
{
iIsDataReleased = ETrue;
if ( iPostDataChunk )
{
delete iPostDataChunk;
iPostDataChunk = NULL;
}
if ( iPostData )
{
delete iPostData;
iPostData = NULL;
}
}
----------------------------------------------------------------
//Post code
TInt CDMDatasyncPost::GetFileToPostL()
{
// This file is internally used hence the hardcoded file name
_LIT( KLogName,"test.txt");
// Changed after client comment for calling Connect() without calling Close() in the destructor
RFs fs;
User::LeaveIfError(fs.Connect());
CleanupClosePushL(fs);
TBuf<KFilePathMaxLength> filePath;
_LIT(KPath,"c:\\data\\");
filePath.Copy(KPath);
filePath.Append(KLogName);
RFile reqFile;
TInt err = reqFile.Open(fs,filePath,EFileRead);
User::LeaveIfError(err);
CleanupClosePushL(reqFile);
if ( iPostData )
{
delete iPostData;
iPostData = NULL;
}
iPostBuffptr.Set(NULL, 0, 0);
delete iPostDataChunk;
iPostDataChunk = NULL;
iPostDataChunk = HBufC8::NewMaxL(KMaxSubmitSize);
iPostBuffptr.Set(iPostDataChunk->Des());
User::LeaveIfError(reqFile.Read(iPostBuffptr));
delete iPostData;
iPostData = NULL;
iPostData = iPostBuffptr.AllocL();
delete iPostDataChunk;
iPostDataChunk = NULL;
User::LeaveIfError(reqFile.Size(iTotalsize));
CleanupStack::PopAndDestroy(2, &fs);
return iTotalsize;
}
-----------------------------------------------------------------
//calling post
TInt CDMHttpDataSync::IssueHttpRequestL( CDMHttpRequestData &aRequestInfo )
{
//Code for Switch case for post
// Open transaction with previous method and parsed uri. This class will
// receive transaction events in MHFRunL and MHFRunError.
iHttpTransaction = iHttpSession.OpenTransactionL( uri, *this, method );
if ( aRequestInfo.iMethod == EIRHttpPOST )
{
TInt FileSize(KErrNone);
TRAPD(err,FileSize=iPostData->GetFileToPostL());
if( err )
{
// failed to open the file to be posted hence cancel the request
CleanupStack::PopAndDestroy(&method);
iHttpTransaction.Close();
return KErrCouldNotConnect;
}
TBuf8<KBufflengthMaxLength> bufflength;
_LIT8(KFormat,"%d");
bufflength.Format(KFormat,FileSize);
aRequestInfo.iContentType.Copy(KPostContentType);
RHTTPRequest LogRequest;
LogRequest = iHttpTransaction.Request();
LogRequest.SetBody((MHTTPDataSupplier&) *iPostData);
RHTTPHeaders header = iHttpTransaction.Request().GetHeaderCollection();
SetHeaderL(header,HTTP::EContentType,aRequestInfo.iContentType );
SetHeaderL(header,HTTP::EContentLength,bufflength);
}
// Setup the headers for the HTTP request
BuildHeadersL(aRequestInfo);
// Submit the transaction. After this the framework will give transaction
// events via MHFRunL and MHFRunError.
iHttpTransaction.SubmitL();
CleanupStack::PopAndDestroy( &method );
iRunning = ETrue;
return KErrNone;
}
-----------------------------------------------------------------
//EGotResponseBodyData in MHFRunL
case THTTPEvent::EGotResponseBodyData:
{
// Part of response's body data received. Use
// aTransaction.Response().Body()->GetNextDataPart() to get the actual
// body data.
// Get the body data supplier
MHTTPDataSupplier *body = aTransaction.Response().Body();
TPtrC8 dataChunk;
body->GetNextDataPart( dataChunk );
iObserver.HttpBodyReceived( dataChunk );
// To release the body data.
body->ReleaseData();
}
break;
hi i have a .txt file in my host. In my application i want to get that .txt file and i want to write that file in my mobile device.
How to do it?
I can able to read the full data from that file. now i want to know how could i write that data into a file. My code is
void CClientAppView::ClientBodyReceived(const TDesC8& aBodyData)
{
HBufC* tempBuf = HBufC::NewL(aBodyData.Length());
CleanupStack::PushL(tempBuf);
tempBuf->Des().Copy(aBodyData);
TPtrC TempPtr=tempBuf->Des();
CleanupStack::PopAndDestroy();
TBuf8<128> buf8;
//if(TempPtr.Left(10)
TInt size = TempPtr.Size();
TInt si = 0;
while(si<size)
{
if(si<1024)
{
buf8.Copy(TempPtr.Mid(si,si + 127));
WriteToFile(buf8);
si += 128;
}
}
here am try to read first 1024 data from that data. But after 128 app wil closed. Can u guide me?
Jayaseelan.V
Dhanus Technologies.,-INDIA.
The second argument of Mid should be a constant 128 (it is length, not position).
Hi,
Thanks for your reply. Whats my problem is, void CClientAppView::ClientBodyReceived(const TDesC8& aBodyData) in this, aBodyData's size is more than 3000..
in my code while reading that data after 512 the application gets terminated. I don't know why it happens..
Please give suggestion to correct my error..
Jayaseelan.V
Dhanus Technologies.,-INDIA.
Hmm, note that PopAndDestroy deletes the buffer for TempPtr, but you have just started using TempPtr by then.
By the way, can it happen that you are trying to implement this method:?Code:void CClientAppView::ClientBodyReceived(const TDesC8& aBodyData) { WriteToFile(aBodyData); }
thanks for ur reply.
I use this too. But in my WriteToFile methaod wad defines as
void CClientAppView::WriteToFile(TDesC8& aContent8)
{
}
If is use WriteToFile(aBodyData) means it through errors like
function call '[CClientAppView].WriteToFile({lval} const TDesC8)' does not match
'CClientAppView::WriteToFile(TDesC8 &)' (non-static)
what can i do now?
Reply soon
Jayaseelan.V
Dhanus Technologies.,-INDIA.
should help, since you cannot modify a TDesC anyway, just the compiler does not know that.Code:void CClientAppView::WriteToFile(const TDesC8& aContent8)
I am able to post the file.That's a great relief for me.