async http upload
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.compactframework.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

éric
Hi,
below is the code I sort of adapted from an async http download example I
found so that I could upload files to the server.
It seems to be doing it asynchronously and the file makes it to the server
but it is going all in one shot.
How can trap it at every 1024 bytes to trigger an event like progress bar
advance as it does in the download version of this code?
When the code calls back uReadCallBack for the first time the entire file is
already on the server?

Any ideas?

-----------Code Start-------------

internal class UploadInfo
{
const int BufferSize = 1024;
internal byte[] BufferWrite;
internal bool useFastBuffers;
internal byte[] dataBufferFast;
internal System.Collections.ArrayList dataBufferSlow;
internal int dataLength;
internal int bytesProcessed;
internal HttpWebRequest Request;
internal Stream ResponseStream;
internal DownloadProgressHandler ProgressCallback;

internal UploadInfo()
{
BufferWrite = new byte[BufferSize];
Request = null;
bytesProcessed = 0;
useFastBuffers = true;
}
}

internal class WebDownload
{
private event System.EventHandler eventStop;
private event ECASOFT.Controls.DownloadError eventError;
internal ManualResetEvent allDone = new ManualResetEvent(false);
internal bool Abort;
private WebRequest req;

const int BUFFER_SIZE = 1024;
internal event System.EventHandler Aborted
{
add {eventStop += new System.EventHandler (value);}
remove {eventStop -= new System.EventHandler (value);}
}
internal event ECASOFT.Controls.DownloadError DownloadError
{
add {eventError += new ECASOFT.Controls.DownloadError (value);}
remove {eventError -= new ECASOFT.Controls.DownloadError (value);}
}
// Start the upload
internal byte[] Upload(
string url, string username, string password, string domain,
DownloadProgressHandler progressCB)
{
try
{
Abort = false;

allDone.Reset();

Uri httpSite = new Uri(url);

FileStream rdr = new FileStream(\\My File.txt, FileMode.Open);

HttpWebRequest req = (HttpWebRequest) WebRequest.Create(url);
req.Method = "PUT";

req.PreAuthenticate = true;
req.Credentials = new NetworkCredential(username, password,
domain);
req.AllowWriteStreamBuffering = true;
req.ContentLength = rdr.Length;
Stream reqStream = req.GetRequestStream();

byte[] inData = new byte[4096];
int bytesRead = rdr.Read(inData, 0, inData.Length);
while (bytesRead > 0)
{
reqStream.Write(inData, 0, bytesRead);
bytesRead = rdr.Read(inData, 0, inData.Length);
}

UploadInfo info = new UploadInfo();
info.Request = (HttpWebRequest) req;
info.dataLength = 1024;
rdr.Close();
reqStream.Close();

info.ProgressCallback += progressCB;

IAsyncResult r = (IAsyncResult) req.BeginGetResponse(new
AsyncCallback(uResponseCallback), info);
allDone.WaitOne();

byte[] data;
if ( info.useFastBuffers )
{
data = info.dataBufferFast;
info.dataBufferFast = null;
}
else
{
data = new byte[ info.dataBufferSlow.Count ];
for ( int b=0; b<info.dataBufferSlow.Count; b++ )
data[b] = (byte) info.dataBufferSlow[b];
info.dataBufferSlow = null;
}
if (this.Abort == false)
{
return data;
}
else
{
return new byte[0];
}
}
catch(System.Exception err)
{
return null;
}
}

private void uResponseCallback(IAsyncResult ar)
{
try
{
UploadInfo info = (UploadInfo) ar.AsyncState;
req = info.Request;
WebResponse resp = req.EndGetResponse(ar);
string strContentLength = resp.Headers["Content-Length"];
if ( strContentLength != null )
{
info.dataLength = Convert.ToInt32( strContentLength );
info.dataBufferFast = new byte[ info.dataLength ];
}
else
{
info.useFastBuffers = false;
info.dataBufferSlow = new System.Collections.ArrayList(
BUFFER_SIZE );
}
Stream ResponseStream = resp.GetResponseStream();
info.ResponseStream = ResponseStream;
IAsyncResult iarWrite =
ResponseStream.BeginRead(info.BufferWrite,
0,
BUFFER_SIZE,
new AsyncCallback(uReadCallBack),
info);
}
catch(System.Exception err)
{
}
}

private void uReadCallBack(IAsyncResult asyncResult)
{
try
{
UploadInfo info = (UploadInfo) asyncResult.AsyncState;
Stream responseStream = info.ResponseStream;
if (Abort == true)
{
req.Abort();
req = null;
System.Object o = new object();
eventStop(o, System.EventArgs.Empty);
allDone.Set();
}
else
{
int bytesWriten = responseStream.EndRead(asyncResult );
if (bytesWriten > 0)
{
if ( info.useFastBuffers )
{
System.Array.Copy(info.BufferWrite, 0,
info.dataBufferFast, info.bytesProcessed,
bytesWriten );
}
else
{
for ( int b=0; b<bytesWriten; b++ )
info.dataBufferSlow.Add( info.BufferWrite[b] );
}
info.bytesProcessed += bytesWriten;
if ( info.ProgressCallback != null )
info.ProgressCallback( info.bytesProcessed,
info.dataLength);
IAsyncResult ar = responseStream.BeginRead(
info.BufferWrite, 0, BUFFER_SIZE,
new AsyncCallback(uReadCallBack), info);
}
else
{
responseStream.Close();
allDone.Set();
}
}
}
catch(System.Exception err)
{
}
}
#endregion

Reply to this message...
 
    
éric
OR if anyone has a sample for HTTP async file upload that would be greatly
appreciated!

éric

"éric" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Alex Feinman [MVP] (VIP)
I think if you set AllowWriteStreamBuffering to false on the outgoing
request, it will send your data as you write them into the output stream.
This will allow you to update progress yourself.

"éric" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Nandal
SEE REMARKS:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemnethttpwebrequestclasspreauthenticatetopic.asp

This is little stupidity in NET CF if preauthenticate is true. Framework does not send HTTP-AUTHORIZATION header in first POST request. Try to make one small GET request only to
make connection and then send data with POST.Otherwise you need to buffer data
to handle 401 WWW-Authenticate Basic response from server.

----- Alex Feinman [MVP] wrote: -----

I think if you set AllowWriteStreamBuffering to false on the outgoing
request, it will send your data as you write them into the output stream.
This will allow you to update progress yourself.

Reply to this message...
 
    
éric
with AllowWriteStreamBuffering = false I get this error :

{"This request requires buffering of data for authentication or redirection
to be sucessful." }

preauthenticate seems to have no affect if true or false

I know I am missing something else... should I be loading the Request Stream
before starting the connection to the server? or should I be somehow loading
slowly bit by bit?
..
..
..
Stream reqStream = req.GetRequestStream();
// Allocate byte buffer to hold file contents
byte[] inData = new byte[4096];
// loop through the local file reading each data block
// and writing to the request stream buffer
int bytesRead = rdr.Read(inData, 0, inData.Length);
while (bytesRead > 0)
{
reqStream.Write(inData, 0, bytesRead);
bytesRead = rdr.Read(inData, 0, inData.Length);
}
..
..
..
allDone.WaitOne();

éric

Reply to this message...
 
    
éric
Can one of you tell me if this is the right logic?

void Upload()
{
Read entire file
Load Request stream with file byte[]
Request method = PUT
IAsyncResult r = (IAsyncResult) req.BeginGetResponse(new AsyncCallback(a, b)
wait for callback
}

"Nandal" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

one small GET request only to
> make connection and then send data with POST.Otherwise you need to buffer
data
[Original message clipped]

Reply to this message...
 
 
System.Array
System.AsyncCallback
System.Collections.ArrayList
System.Convert
System.EventArgs
System.EventHandler
System.Exception
System.IAsyncResult
System.IO.File
System.IO.FileMode
System.IO.FileStream
System.Net.HttpWebRequest
System.Net.NetworkCredential
System.Net.WebRequest
System.Net.WebResponse
System.Object
System.Threading.ManualResetEvent
System.Uri




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com