示例#1
0
        private static byte[] PrepareContents(Hashtable formVariables, string boundary)
        {
            MemoryStream stream = new MemoryStream();

            using (BinaryWriter writer = new BinaryWriter(stream))
            {
                IDictionaryEnumerator enumerator = formVariables.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    string key = ((WebPage.FormKey)enumerator.Key).Name.Trim();
                    object val = enumerator.Value;

                    HtmlFileInfo file = val as HtmlFileInfo;
                    if (file != null)
                    {
                        if (file.Filename.Trim().Length > 0)
                        {
                            writer.Write(encoding.GetBytes(String.Format("--{0}\r\n", boundary)));
                            writer.Write(encoding.GetBytes(String.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n",
                                                                         file.Name.Trim(), file.Filename)));

                            writer.Write(encoding.GetBytes("Content-Type: application/octet-stream"));
                            writer.Write(encoding.GetBytes("\r\n\r\n"));
                            file.WriteContents(writer);
                            writer.Write(encoding.GetBytes("\r\n"));
                        }
                    }
                    else
                    {
                        writer.Write(encoding.GetBytes(String.Format("--{0}\r\n", boundary)));
                        writer.Write(encoding.GetBytes(String.Format("Content-Disposition: form-data; name=\"{0}\"\r\n", key)));

                        writer.Write(encoding.GetBytes("\r\n"));
                        writer.Write(encoding.GetBytes(val.ToString()));
                        writer.Write(encoding.GetBytes("\r\n"));
                    }
                }
                writer.Write(encoding.GetBytes(String.Format("--{0}--\r\n", boundary)));
            }

            return(stream.ToArray());
        }
示例#2
0
文件: WebPage.cs 项目: formist/LinkMe
        public void SetFormVariable(object owner, string name, string value)
        {
            if (owner == null)
            {
                throw new ArgumentNullException("owner");
            }

            XmlElement element = owner as XmlElement;

            if (element != null)
            {
                string type = element.GetAttribute("type");

                if (type == "file")
                {
                    formVariables[new FormKey(owner, name)] = new HtmlFileInfo(name, value);
                    return;
                }
            }

            formVariables[new FormKey(owner, name)] = value;
        }