I am having a real odd issue. I am POSTing a Form (with fields and a
file upload) to a server using HttpWebRequest. I am including the
Field populted with XML in the payload of the HTTP POST just before
the File, not in the query string.

The problem I am having is that the XML is being encoded with the \"
characters instead of " chars as follows

 <request><service name=\"system.logon\" session=
\"123\"><args><host>some value</host>...

instead of

 <request><service name="system.logon" session="123"><args><host>some
value</host>...

When the Xml is viewed in Debug the " chars are slash escaped and when
viewed in the Debug Object Value Text Viewer or Xml Viewer all is
fine. However the \" chars are coming through in the encoded XML at
the other end as the form is being read.

Any thoughts as to why this "escaping" of the quote char is being
carried through into the byte array and being sent to the server ?

Code fragment which is creating the POST request.

================================================

  string serviceRequest = someXmlDocument.InnerXml;

  string boundary = "---------------------" +
DateTime.Now.Ticks.ToString("x");
  byte[] boundaryBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--"
+ boundary + "\r\n");

  HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri
(this.Host));
  webRequest.CookieContainer = new CookieContainer();
  webRequest.ContentType = "multipart/form-data; boundary=" +
boundary;
  webRequest.Method = "POST";

  StringBuilder sb = new StringBuilder();
  sb.Append("--" + boundary + "\r\n");
  sb.Append("Content-Disposition: form-data; name=\"request\"\r\n\r
\n");
  sb.Append(serviceRequest + "\r\n");
  sb.Append("--" + boundary + "\r\n");
  sb.Append("Content-Disposition: form-data; name=\"file1\"; filename=
\"some-filename.jpg\"\r\n");
  sb.Append("Content-Type: application/octet-stream\r\n\r\n");
  string postHeader = sb.ToString();
  byte[] postHeaderBytes = UTF8Encoding.UTF8.GetBytes(postHeader);

  FileStream fileStream = new FileStream(pathToFile, FileMode.Open,
FileAccess.Read);
  webRequest.ContentLength = postHeaderBytes.Length +
fileStream.Length + boundaryBytes.Length;
  Stream requestStream = webRequest.GetRequestStream();
  requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
  byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)
fileStream.Length))];
  int bytesRead = 0;
  while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
  {
      requestStream.Write(buffer, 0, bytesRead);
  }
  requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);

  //... Read from Response Stream
================================================

 Any help greatfully appreciated...

Al

Reply via email to