Sending raw data to a web server

J

JoeB

hi,

Im trying to send data (raw bytes) to a web server. I cannot figure out how
to do it. I need the data to be read by PHP which i have already written.
The data must be post data.

I also send some variables to the web server, which i can retreive, but the
raw data disapears.

Here is my sending code (error checking removed):
--------

string boundary = "----------" + DateTime.Now.Ticks.ToString("x");

string sRequestUrl = this.sUrl + "?key=12345";

HttpWebRequest webrequest = CreateWebRequest( sRequestUrl );

webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
webrequest.Method = "POST";
webrequest.AllowWriteStreamBuffering = false;

// Build up the post message header
StringBuilder sb = new StringBuilder();
sb.Append("--" + boundary + "\r\n");
sb.Append("Content-Type: text/plain; name=\"data\"");
sb.Append("Content-Type: application/octet-stream");
sb.Append("\r\n\r\n");

byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sb.ToString());

byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary +
"\r\n");

// Read file data.
byte[] buffer = new Byte[ this.iUploadBufferSize ];
long lBytesReadFromFile = fs.Read(buffer, (int)lOffset, buffer.Length);

// set request length.
webrequest.ContentLength = postHeaderBytes.Length + lBytesReadFromFile +
boundaryBytes.Length;

Stream requestStream = null;
requestStream = webrequest.GetRequestStream();

// Write out our post header
requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);

// Write out the file contents
requestStream.Write( buffer, 0, (int)lBytesReadFromFile );

// Write out the trailing boundary
requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);

requestStream.Flush();
requestStream.Close();


--------





please help!!



Joe
 
K

koltun

Hi,

Why do you have the "Content-Type" specified twice ?
sb.Append("Content-Type: text/plain; name=\"data\"");
sb.Append("Content-Type: application/octet-stream");

Also try to put "Content-Transfer-Encoding: binary" in the headers of
mime part, and put a "--" trail in the closing boundary.

Hope it will help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top