Example #1
0
        /// <summary>
        /// 獲得postdata的串
        /// </summary>
        /// <returns></returns>
        private object GetPostDataData()
        {
            if (_contenttype.ToLower().IndexOf("/json") > 0)
            {
                return(_postdata.ToJSONString());
            }
            else if (_contenttype == "multipart/form-data")
            {
                /*
                 * multipart/form-data的请求内容格式如下
                 * Content-Type: multipart/form-data; boundary=AaB03x
                 *
                 * --boundary
                 * Content-Disposition: form-data; name="submit-name"
                 *
                 * Larry
                 * --boundary
                 * Content-Disposition: form-data; name="file"; filename="file1.dat"
                 * Content-Type: application/octet-stream
                 *
                 * ... contents of file1.dat ...
                 * --boundary--
                 * */

                var boundary = String.Format("----------{0:N}", Guid.NewGuid());
                _contenttype = _contenttype + ";boundary=" + boundary;

                Stream formDataStream = new System.IO.MemoryStream();

                foreach (var k in _postdata.Keys)
                {
                    var item = _postdata.GetValue(k);
                    if (item is FrameDLRObject)
                    {
                        dynamic dobj = item;
                        if (ComFunc.nvl(dobj.contenttype) == "application/octet-stream")
                        {
                            var name            = ComFunc.nvl(dobj.name);
                            var filename        = ComFunc.nvl(dobj.filename);
                            var filecontenttype = ComFunc.nvl(dobj.filecontenttype);
                            var filecontent     = (byte[])dobj.formitem;

                            // Add just the first part of this param, since we will write the file data directly to the Stream
                            string header = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"\r\nContent-Type: {3}\r\n\r\n",
                                                          boundary,
                                                          name,
                                                          filename,
                                                          filecontenttype != "" ? filecontenttype : "application/octet-stream");

                            formDataStream.Write(_encoding.GetBytes(header), 0, _encoding.GetByteCount(header));

                            // Write the file data directly to the Stream, rather than serializing it to a string.
                            formDataStream.Write(filecontent, 0, filecontent.Length);
                        }
                        else
                        {
                            var name     = ComFunc.nvl(dobj.name);
                            var formitem = ComFunc.nvl(dobj.formitem);
                            // Add just the first part of this param, since we will write the file data directly to the Stream
                            string header = string.Format("--{0}\r\nContent-Disposition: form-data; name={1}\r\n\r\n",
                                                          boundary,
                                                          name);

                            formDataStream.Write(_encoding.GetBytes(header), 0, _encoding.GetByteCount(header));
                            var bytes = Encoding.UTF8.GetBytes(formitem);
                            // Write the file data directly to the Stream, rather than serializing it to a string.
                            formDataStream.Write(bytes, 0, bytes.Length);
                        }
                    }
                }
                // Add the end of the request.  Start with a newline
                string footer = "\r\n--" + boundary + "--\r\n";
                formDataStream.Write(_encoding.GetBytes(footer), 0, _encoding.GetByteCount(footer));

                // Dump the Stream into a byte[]
                formDataStream.Position = 0;
                byte[] formData = new byte[formDataStream.Length];
                formDataStream.Read(formData, 0, formData.Length);
                formDataStream.Close();

                return(formData);
            }//以put的方式发送文件内容
            else if (_contenttype.ToLower().StartsWith("put/"))
            {
                if (_postdata.Keys.Count > 0)
                {
                    var item = _postdata.GetValue(_postdata.Keys[0]);
                    if (item is FrameDLRObject)
                    {
                        dynamic dobj = item;
                        //只处理第一笔资料
                        //var name = ComFunc.nvl(dobj.name);
                        //var filename = ComFunc.nvl(dobj.filename);
                        //var filecontenttype = ComFunc.nvl(dobj.filecontenttype);
                        var filecontent = (byte[])dobj.formitem;

                        return(filecontent);
                    }
                    else if (item is byte[])
                    {
                        return((byte[])item);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    return(null);
                }
            }
            else if (_contenttype.ToLower().IndexOf("/xml") > 0)
            {
                return(_postdata.ToXml());
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                foreach (string s in _postdata.Keys)
                {
                    sb.AppendFormat("&{0}={1}", s, _postdata.GetValue(s));
                }
                return(sb.Length > 0 ? sb.ToString().Substring(1) : "");
            }
        }