예제 #1
0
파일: Factory.cs 프로젝트: wyerp/.NET-sdk
        private byte[] UploadFiles(string address, UploadFile file, Dictionary<string,string> values)
        {
            var request = WebRequest.Create(address);
            request.Method = "POST";
            foreach (var header in headers)
            {
                if (header.Key == "Content-type") continue;
                request.Headers.Add(header.Key, header.Value);
            }
            var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);
            request.ContentType = "multipart/form-data; boundary=" + boundary;
            boundary = "--" + boundary;

            using (var requestStream = request.GetRequestStream())
            {
                // Write the values
                foreach (string name in values.Keys)
                {
                    var kbuffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
                    requestStream.Write(kbuffer, 0, kbuffer.Length);
                    kbuffer = Encoding.ASCII.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"{1}{1}", name, Environment.NewLine));
                    requestStream.Write(kbuffer, 0, kbuffer.Length);
                    kbuffer = Encoding.UTF8.GetBytes(values[name] + Environment.NewLine);
                    requestStream.Write(kbuffer, 0, kbuffer.Length);
                }

                var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
                requestStream.Write(buffer, 0, buffer.Length);
                buffer = Encoding.UTF8.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}", file.Name, file.Filename, Environment.NewLine));
                requestStream.Write(buffer, 0, buffer.Length);
                buffer = Encoding.ASCII.GetBytes(string.Format("Content-Type: {0}{1}{1}", file.ContentType, Environment.NewLine));
                requestStream.Write(buffer, 0, buffer.Length);
                file.Stream.CopyTo(requestStream);
                buffer = Encoding.ASCII.GetBytes(Environment.NewLine);
                requestStream.Write(buffer, 0, buffer.Length);

                var boundaryBuffer = Encoding.ASCII.GetBytes(boundary + "--");
                requestStream.Write(boundaryBuffer, 0, boundaryBuffer.Length);
            }

            using (var response = request.GetResponse())
            using (var responseStream = response.GetResponseStream())
            using (var stream = new MemoryStream())
            {
                responseStream.CopyTo(stream);
                return stream.ToArray();
            }
        }
예제 #2
0
파일: Factory.cs 프로젝트: wyerp/.NET-sdk
 private string Upload(string url, FileStream stream, Dictionary<string, string> map)
 {
     var file = new UploadFile
     {
         Name = "file",
         Filename = Path.GetFileName(stream.Name),
         ContentType = "image/png",
         Stream = stream
     };
     if (map == null) map = new Dictionary<string, string>();
     map.Add("filename", Path.GetFileName(stream.Name));
     byte[] result = UploadFiles(url, file, map);
     string ResultStr = System.Text.Encoding.GetEncoding("utf-8").GetString(result);//解码  
     return ResultStr;
 }