Sending raw data to a web server

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JoeB

    Sending raw data to a web server

    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.Ti cks.ToString("x ");

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

    HttpWebRequest webrequest = CreateWebReques t( sRequestUrl );

    webrequest.Cont entType = "multipart/form-data; boundary=" + boundary;
    webrequest.Meth od = "POST";
    webrequest.Allo wWriteStreamBuf fering = false;

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

    byte[] postHeaderBytes = Encoding.UTF8.G etBytes(sb.ToSt ring());

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

    // Read file data.
    byte[] buffer = new Byte[ this.iUploadBuf ferSize ];
    long lBytesReadFromF ile = fs.Read(buffer, (int)lOffset, buffer.Length);

    // set request length.
    webrequest.Cont entLength = postHeaderBytes .Length + lBytesReadFromF ile +
    boundaryBytes.L ength;

    Stream requestStream = null;
    requestStream = webrequest.GetR equestStream();

    // Write out our post header
    requestStream.W rite(postHeader Bytes, 0, postHeaderBytes .Length);

    // Write out the file contents
    requestStream.W rite( buffer, 0, (int)lBytesRead FromFile );

    // Write out the trailing boundary
    requestStream.W rite(boundaryBy tes, 0, boundaryBytes.L ength);

    requestStream.F lush();
    requestStream.C lose();


    --------





    please help!!



    Joe


  • koltun@gmail.com

    #2
    Re: Sending raw data to a web server

    Hi,

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

    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.

    Comment

    Working...