C
cyberco
Using: .Net Compact Framework 2, Windows Mobile 5 (Pocket PC)
I'm trying to upload a large binary file in chuncks over a socket (as a
multipart mime message). This is how I'm trying to accomplish this, but
it somehow doesn't work:
================================================
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
IPAddress ipa = Dns.GetHostEntry("www.blablabla.com").AddressList[0];
IPEndPoint ipe = new IPEndPoint(ipa, 80);
s.Connect(ipe);
FileStream tmpFileStream = new FileStream(tmpUploadFileName,
FileMode.Open, FileAccess.Read);
StringBuilder postString = new StringBuilder("POST /servlet
HTTP/1.1\r\n");
postString.Append("Host: www.blablabla.com\r\n");
postString.Append("User-Agent: Windows Mobile 5 PPC\r\n");
postString.Append("Accept: */*;q=0.5\r\n");
postString.Append("Keep-Alive: 300\r\n");
postString.Append("Connection: keep-alive\r\n");
postString.Append("Content-Type: multipart/form-data;
boundary=----1234----\r\n");
postString.Append("Content-Length: " + tmpFileStream.Length + "\r\n");
postString.Append("\r\n");
postString.Append("\r\n");
s.Send(Encoding.UTF8.GetBytes(postString.ToString()));
int numRead = 1;
int chunkSize = 1024;
byte[] bytes = new byte[chunkSize];
while ((numRead = tmpFileStream.Read(bytes, 0, chunkSize)) > 0) {
try {
s.Send(bytes, numRead, SocketFlags.None);
} catch (Exception e) {
}
}
s.Shutdown(SocketShutdown.Send);
if (s.Connected) {
s.Close();
}
================================================
If I read in the complete file and upload it at once everything goes
fine, so it's likely not a problem with the format of the multipart
mime message.
I find the documentation on FileStream.Read() a bit vague, saying that
the 'offset' parameter is the offset in 'aray', which is the
destination array. It says 'offset: The byte offset in array at which
to begin reading. ' (where 'array' is the name of the destination array
parameter). Reading from the destination array?
I'm trying to upload a large binary file in chuncks over a socket (as a
multipart mime message). This is how I'm trying to accomplish this, but
it somehow doesn't work:
================================================
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
IPAddress ipa = Dns.GetHostEntry("www.blablabla.com").AddressList[0];
IPEndPoint ipe = new IPEndPoint(ipa, 80);
s.Connect(ipe);
FileStream tmpFileStream = new FileStream(tmpUploadFileName,
FileMode.Open, FileAccess.Read);
StringBuilder postString = new StringBuilder("POST /servlet
HTTP/1.1\r\n");
postString.Append("Host: www.blablabla.com\r\n");
postString.Append("User-Agent: Windows Mobile 5 PPC\r\n");
postString.Append("Accept: */*;q=0.5\r\n");
postString.Append("Keep-Alive: 300\r\n");
postString.Append("Connection: keep-alive\r\n");
postString.Append("Content-Type: multipart/form-data;
boundary=----1234----\r\n");
postString.Append("Content-Length: " + tmpFileStream.Length + "\r\n");
postString.Append("\r\n");
postString.Append("\r\n");
s.Send(Encoding.UTF8.GetBytes(postString.ToString()));
int numRead = 1;
int chunkSize = 1024;
byte[] bytes = new byte[chunkSize];
while ((numRead = tmpFileStream.Read(bytes, 0, chunkSize)) > 0) {
try {
s.Send(bytes, numRead, SocketFlags.None);
} catch (Exception e) {
}
}
s.Shutdown(SocketShutdown.Send);
if (s.Connected) {
s.Close();
}
================================================
If I read in the complete file and upload it at once everything goes
fine, so it's likely not a problem with the format of the multipart
mime message.
I find the documentation on FileStream.Read() a bit vague, saying that
the 'offset' parameter is the offset in 'aray', which is the
destination array. It says 'offset: The byte offset in array at which
to begin reading. ' (where 'array' is the name of the destination array
parameter). Reading from the destination array?