在.net中创建这样的帖子请求的最佳方法是什么? 我尝试使用HttpClient和MultipartFormDataContent类,但到目前为止我只能上传图像。如何添加像api key这样的附加参数?
POST /services/upload/ HTTP/1.1
Content-Type: multipart/form-data; boundary=---------------------------7d44e178b0434
Host: api.flickr.com
Content-Length: 35261
-----------------------------7d44e178b0434
Content-Disposition: form-data; name="api_key"
3632623532453245
-----------------------------7d44e178b0434
Content-Disposition: form-data; name="auth_token"
436436545
-----------------------------7d44e178b0434
Content-Disposition: form-data; name="api_sig"
43732850932746573245
-----------------------------7d44e178b0434
Content-Disposition: form-data; name="photo"; filename="C:\test.jpg"
Content-Type: image/jpeg
{RAW JFIF DATA}
-----------------------------7d44e178b0434--
答案 0 :(得分:0)
C#+ .NET 4.0中的某些内容
Uri uri = new Uri("api.flickr.com");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
//goes into the body. not header
NameValueCollection queryString = HttpUtility.ParseQueryString(string.Empty);
queryString["api_key"] = "7d44e178b0434";
...
object returnValue = null;
try
{
using (var writer = new StreamWriter(request.GetRequestStream()))
{
writer.WriteLine(queryString);
}
returnValue = request.GetResponse();
}
catch (Exception ex)
{
..
}
如果您想在标题中添加内容 -
request.Headers.Add("HeaderKEY", somevalue);