Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#HTTP套接字文件POST?_C#_File_Http_Sockets_Post - Fatal编程技术网

C#HTTP套接字文件POST?

C#HTTP套接字文件POST?,c#,file,http,sockets,post,C#,File,Http,Sockets,Post,我想通过HTTP将一个文件上传到PHP脚本,我想通过套接字来完成。帖子已经运行良好,只是文件发送不正常 首先,我构建HTTP请求,直到文件开始,然后将该字符串转换为ByteArray。使用Buffer.BlockCopy我将文件放在这个数组的末尾。使用另一个区块复制,我将末端边界放在数组的末端。在此之后,我尝试将ByteArray作为请求发送(带有标头),但看起来文件没有发送,或者格式错误 以下是我代码的一部分: _content = _content + "--" + bound

我想通过HTTP将一个文件上传到PHP脚本,我想通过套接字来完成。帖子已经运行良好,只是文件发送不正常

首先,我构建HTTP请求,直到文件开始,然后将该字符串转换为ByteArray。使用
Buffer.BlockCopy
我将文件放在这个数组的末尾。使用另一个区块复制,我将末端边界放在数组的末端。在此之后,我尝试将ByteArray作为请求发送(带有标头),但看起来文件没有发送,或者格式错误

以下是我代码的一部分:

 _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;
    }

有人知道这个问题并能帮助我吗?

为什么不使用
WebClient
类(请参阅
UploadData
方法)或至少
HttpWebRequest
类?因为我想通过套接字来实现,并且想避免WebClient和HttpWebRequest。然后继续尝试重新发明轮子。感谢这些帮助性评论。对于低级C++ HTTP类来说,它有点预构建,它与“重新发明轮子”无关,它是训练。这些限制应该是电子邮件MIME(如果我记得正确的话),当我用Fibug来检查文件请求时,它以原始格式显示文件。