Ejemplo n.º 1
0
 /// Creates a single string of name/value pairs from the
 /// Attributes collection of the supplied RestDataItem
 /// pairs are delimited by semi-colons.
 /// Example: name="value"; name-1="value-1"; name-2="value-2"
 private string GetAttributeString(HttpDataSource dataSource)
 {
     if (dataSource.Attributes != null)
     {
         StringBuilder attributeBuilder = new StringBuilder();
         for (int i = 0; i < dataSource.Attributes.Count; i++)
         {
             attributeBuilder.AppendFormat("{0}=\"{1}\"{2}",
                                           dataSource.Attributes.Keys[i],
                                           dataSource.Attributes[dataSource.Attributes.Keys[i]],
                                           (i < (dataSource.Attributes.Count - 1)) ? "; " : string.Empty);
         }
         return(attributeBuilder.ToString());
     }
     return(null);
 }
Ejemplo n.º 2
0
        private void AppendDataItem(Stream stream, HttpDataSource dataItem, string boundary, Encoding encoding)
        {
            StringBuilder dataItemBuilder = new StringBuilder();

            byte[] newlineBytes = encoding.GetBytes("\r\n");
            dataItemBuilder.Append("--" + boundary + "\r\n");
            dataItemBuilder.AppendFormat("Content-Disposition: form-data; {0}\r\n", GetAttributeString(dataItem));
            dataItemBuilder.AppendFormat("Content-Type: {0}\r\n", dataItem.ContentType);
            dataItemBuilder.Append("\r\n");
            stream.Write(encoding.GetBytes(dataItemBuilder.ToString()), 0, encoding.GetByteCount(dataItemBuilder.ToString()));
            if (dataItem.Data != null)
            {
                stream.Write(dataItem.Data, 0, dataItem.Data.Length);
            }
            stream.Write(newlineBytes, 0, newlineBytes.Length);
        }