C#套接字文件传输POST方法

时间:2012-01-10 18:07:10

标签: c# sockets post webserver file-transfer

我要做的是用c#编写一个简单的HTTP守护进程。我设法支持GET方法,但POST方法似乎更难。我必须解析multipart formdata并保存已通过FILE Upload发送的服务器上的文件。

我实际在做什么:

  1. 检查SocketBuffer Substring是否包含POST方法
  2. 寻找边界标签 &安培;提取它
  3. 从boundarytag读取标题到边界标记 同时保存数组中这两个标记之间的子字符串。
  4. 解析每个阵列字段 - 检查它是否包含任何数据 保存在磁盘上。
  5. 如果它包含数据:阅读 “content-disposition”,“filename”,“content-type”和数据 本身。
  6. public string parse_boundary(string boundary)             {                 if(contains_data(boundary))                 {                     get_content_disposition(边界);                     get_filename(边界);                     get_contenttype(边界);                     get_binarydata(边界);                 }                 其他                 {                     Console.WriteLine(“No DATA!”);                 }                 返回“”;             }

            public string get_content_disposition(string substring)
            {
                int first, last;
                first = substring.IndexOf("Content-Disposition") + "Content-Disposition:".Length;
                last = substring.IndexOf("Content-Type:");
                string content_disposition = substring.Substring(first, (last - first));
                content_disposition = content_disposition.Replace("\t", "").Replace("\r", "").Replace("\n","");
    
    #if DEBUG_MODE
                Console.WriteLine("DEBUG content_disposition: " + content_disposition);
    #endif
                return content_disposition;
            }
    
    
    
    
    
            public string get_filename(string substring)
            {
                int first, last;
                first = substring.IndexOf("filename=\"") + "filename=\"".Length;
                last = substring.IndexOf("\"", first);
                string filename = substring.Substring(first, (last - first));
    
    #if DEBUG_MODE
                Console.WriteLine("DEBUG Filename: " + filename);
    #endif
                return filename;
            }
    
    
    
    
    
            public string get_contenttype(string substring)
            {
                int first, last;
                first = substring.IndexOf("Content-Type:", substring.IndexOf("Content-Disposition") + "Content_Type:".Length);
                last = substring.IndexOf("\n", first);
                string content_type = substring.Substring(first, (last - first));
                content_type = content_type.Trim();
    
    #if DEBUG_MODE
                Console.WriteLine("DEBUG Content-Type: " + content_type);
    #endif
                return content_type;
            }
    
    
    
    
    
    
            public void get_binarydata(string substring)
            {
                int first, second, last;
                first = substring.IndexOf("Content-Type:", substring.IndexOf("Content-Disposition") + "Content_Type:".Length);
                second = substring.IndexOf("\n", first);
                last = substring.Length;
                string tobinary = substring.Substring(second, (last - second));
                //tobinary = tobinary.TrimStart();
    #if DEBUG_MODE
                Console.WriteLine("DEBUG get_binarydata: " + tobinary);
    #endif
                byte[] encoded_binary = System.Text.Encoding.UTF8.GetBytes(tobinary);
                ByteArrayToFile(get_filename(substring), encoded_binary);
            }
    
            public bool ByteArrayToFile(string _Filename, byte[] _ByteArray)
            {
                try
                {
                    System.IO.FileStream _FileStream = new System.IO.FileStream(_Filename, System.IO.FileMode.Create, System.IO.FileAccess.Write);
                    _FileStream.Write(_ByteArray, 0, _ByteArray.Length);
                    _FileStream.Close();
    
                    return true;
                }
                catch (Exception _Exception)
                {
                    Console.WriteLine("Exeception caught in process: {0}", _Exception.ToString());
                }
                return false;
            }
    
    
    
    
    
            public bool contains_data(string boundary)
            {
                int idx = boundary.IndexOf("Content-Type:");
                if (idx == -1)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
    

    我知道必须有更多的try / catch块,但这只是实验而不是生产用途。我还要提到我的BufferSize是[1024 * MAX_UPLOAD_SIZE](目前设置为2MB)。

    现在我的问题:当我尝试通过文件上传上传图像时,我的图像文件总是2MB大小(BufferSize)。当我尝试上传除txt文件之外的任何其他数据时,文件未完全解码且无法使用。任何想法如何解决这个问题?

0 个答案:

没有答案
相关问题