Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.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#_Http_Post - Fatal编程技术网

C# 无法使用HTTP POST上载文件

C# 无法使用HTTP POST上载文件,c#,http,post,C#,Http,Post,我试图使用HTTPPOST上传一个文件,但在服务器端处理请求时,不知何故找不到文件。我能够创建一个类似的请求,并使用Chrome的Postman扩展名成功上传文件,但不知何故,无法通过编程实现同样的操作 客户端代码: HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fullUrl); request.Method = "POST"; using (Stream requestStream = request.GetReq

我试图使用HTTPPOST上传一个文件,但在服务器端处理请求时,不知何故找不到文件。我能够创建一个类似的请求,并使用Chrome的Postman扩展名成功上传文件,但不知何故,无法通过编程实现同样的操作

客户端代码:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fullUrl); 
request.Method = "POST"; 

using (Stream requestStream = request.GetRequestStream()) 
{
   string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
   byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("--" + boundary + "\r\n");
   byte[] trailer = System.Text.Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
   string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type:{2}\r\n\r\n";
   string header = string.Format(headerTemplate, "Files", "myFile.xml", "text/xml");
   byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
   requestStream.Write(boundarybytes, 0, boundarybytes.Length);
   requestStream.Write(headerbytes, 0, headerbytes.Length);                    
   requestStream.Write(uploadedFile, 0, uploadedFile.Length);
   requestStream.Write(trailer, 0, trailer.Length);

}     
        var httpRequest = HttpContext.Current.Request;
        if (httpRequest.Files.Count != 1)
            return BadRequest("Didn't get the file.");
请求如下所示(在Fiddler中):

POST https://host/myUrl
Content-Length: 1067
Expect: 100-continue
Connection: Keep-Alive

------------8d2942f79ab208e
Content-Disposition: form-data; name="Files"; filename="myFile.xml"
Content-Type:text/xml

<myFile>
  Something
</myFile>

------------8d2942f79ab208e
但是我总是得到
httpRequest.Files.Count
为零。为什么?

下面的请求(使用Postman创建)将
httpRequest.Files.Count
设为一个,如预期的那样

POST myUrl HTTP/1.1
Host: host
Cache-Control: no-cache

----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="Files"; filename="myFile.xml"
Content-Type: text/xml


----WebKitFormBoundaryE19zNvXGzXaLvS5C

我做错了什么?

也许您需要将请求的内容类型设置为“多部分/表单数据”


我明白了。多亏了这个

进行了两项更改:

POST https://host/myUrl
Content-Length: 1067
Expect: 100-continue
Connection: Keep-Alive

------------8d2942f79ab208e
Content-Disposition: form-data; name="Files"; filename="myFile.xml"
Content-Type:text/xml

<myFile>
  Something
</myFile>

------------8d2942f79ab208e
1) 添加内容类型:

POST https://host/myUrl
Content-Length: 1067
Expect: 100-continue
Connection: Keep-Alive

------------8d2942f79ab208e
Content-Disposition: form-data; name="Files"; filename="myFile.xml"
Content-Type:text/xml

<myFile>
  Something
</myFile>

------------8d2942f79ab208e
   request.ContentType = "multipart/form-data; boundary=" + boundary;
2) 修改了边界的结束方式

byte[] trailer = System.Text.Encoding.UTF8.GetBytes("\r\n--" + boundary + "--");

现在它可以工作了。希望这对某人有所帮助。

尝试过,但没有帮助。也许可以研究一下使用方法,看看它是否符合您的要求