Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.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# 如何使本地文件成为HttpWebRequest的主体?_C#_Post_Curl_Httpwebrequest_Webrequest - Fatal编程技术网

C# 如何使本地文件成为HttpWebRequest的主体?

C# 如何使本地文件成为HttpWebRequest的主体?,c#,post,curl,httpwebrequest,webrequest,C#,Post,Curl,Httpwebrequest,Webrequest,例如: 我必须根据这个发布数据 他们要求发布一个以本地文件为主体的请求 他们建议的curl是:curl-i-data binary@test.mp3http://developer.doreso.com/api/v1 但是我如何在c中做到这一点呢?尝试使用HttpWebRequest类并在多部分/表单数据请求中发送文件 下面是一个示例代码,您可以对其进行一些修改 首先读取文件的内容: byte[] fileToSend = File.ReadAllBytes(@"C:\test.mp3");

例如:

我必须根据这个发布数据 他们要求发布一个以本地文件为主体的请求

他们建议的curl是:curl-i-data binary@test.mp3http://developer.doreso.com/api/v1

但是我如何在c中做到这一点呢?

尝试使用HttpWebRequest类并在多部分/表单数据请求中发送文件

下面是一个示例代码,您可以对其进行一些修改

首先读取文件的内容:

byte[] fileToSend = File.ReadAllBytes(@"C:\test.mp3"); 
然后准备HttpWebRequest对象:

将文件作为正文请求发送:

using (Stream requestStream = request.GetRequestStream())
{ 
    requestStream.Write(fileToSend, 0, fileToSend.Length);
    requestStream.Close();
}
阅读回答,然后:

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string result;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
    result = reader.ReadToEnd();
}
如果需要,请使用响应:

Console.WriteLine(result);

不要,尤其是当代码不起作用时,是的,我看到你忍者编辑了相似性。您也不希望一次读取一个字节数组中的整个文件。快速查看文档后会发现内容类型应该是application/octet stream,而不是multipart/form data谢谢@ry8806,我已经编辑了我的答案。
Console.WriteLine(result);