【发布时间】:2013-12-17 15:57:06
【问题描述】:
我被要求在 C# 中执行以下操作:
/**
* 1. Create a MultipartPostMethod
* 2. Construct the web URL to connect to the SDP Server
* 3. Add the filename to be attached as a parameter to the MultipartPostMethod with parameter name "filename"
* 4. Execute the MultipartPostMethod
* 5. Receive and process the response as required
* /
我写了一些没有错误的代码,但是文件没有附加。
有人可以看看我的 C# 代码,看看我是否写错了代码?
这是我的代码:
var client = new HttpClient();
const string weblinkUrl = "http://testserver.com/attach?";
var method = new MultipartFormDataContent();
const string fileName = "C:\file.txt";
var streamContent = new StreamContent(File.Open(fileName, FileMode.Open));
method.Add(streamContent, "filename");
var result = client.PostAsync(weblinkUrl, method);
MessageBox.Show(result.Result.ToString());
【问题讨论】:
-
这已在 SO 上被多次询问。以下是一些可能的解决方案: C# HttpClient 4.5 multipart/form-data upload:stackoverflow.com/questions/16416601/… HttpClient Multipart Form Post in C#:stackoverflow.com/questions/18059588/… 个人注意,检查请求中发送的 post 数据,并检查响应。 Fiddler 非常适合这个。
标签: c# post httpclient filestream multipartform-data