C#HTTP套接字文件POST?

时间:2012-08-09 13:56:35

标签: c# file http sockets post

我想通过HTTP将文件上传到PHP脚本,我想通过套接字来完成。邮政已经正常工作只有文件似乎没有正确发送。

首先,我构建我的HTTP请求,直到文件的开头,然后我将此字符串转换为ByteArray。使用Buffer.BlockCopy我将文件放在此数组的末尾。使用另一个BlockCopy,我将结束边界放在数组的末尾。在此之后,我尝试将我的ByteArray作为Request(带标题)发送,但看起来文件不发送,或格式错误。

以下是我的代码的一部分:

 _content = _content
      + "--" + boundary + Environment.NewLine
      + "Content-Disposition: form-data; name=\"" + this._FileVarName + "\"; filename=\"" + Path.GetFileName(this._FilePath) + "\""
      + Environment.NewLine + "Content-Transfer-Encoding: application/octet-stream"
      + Environment.NewLine + Environment.NewLine;

    mainContent = this.Combine(Encoding.UTF8.GetBytes(_content), System.IO.File.ReadAllBytes(this._FilePath));
    mainContent = this.Combine(mainContent, Encoding.UTF8.GetBytes(Environment.NewLine + "--" + boundary + "--"));

    private byte[] Combine(byte[] first, byte[] second)
    {
      byte[] ret = new byte[first.Length + second.Length];
      Buffer.BlockCopy(first, 0, ret, 0, first.Length);
      Buffer.BlockCopy(second, 0, ret, first.Length, second.Length);
      return ret;
    }

有谁知道这个问题,可以帮助我?

0 个答案:

没有答案