在c#中使用webrequest发布一张图片
本文关键字:布一张 webrequest | 更新日期: 2023-09-27 17:51:02
我有以下python代码如何通过webservice发布图片:
product_image = requests.post(
'https://client.planorama.com/tapi/v1/product_image/',
data={ 'product_id': 1784682 },
files={ "file": open(my_image.jpg, 'rb') }
)
谁能帮我在c#中做同样的事情,
c#中的多部分表单Post包含一个简单的c#类,使用HttpWebRequest和一个提供的(工作的)示例。
我们被python中的requests
库宠坏了。
你运行的是。net 4还是4.5?如果是这样,看看Joshcodes对在c#中使用HTTP POST发送文件的回答-它使用了Microsoft.Net.Http
,这是目前为止在。net世界中最好的HTTP库。
更新:我还没有检查这个准确性,但它可能是这样的:
static HttpResponseMessage UploadFileWithParam(string requestUri, string fileName, string key1, string val1)
{
using (var client = new HttpClient())
{
using (var content = new MultipartFormDataContent())
{
content.Add(new StringContent(val1), key1);
var fileContent = new StreamContent(File.OpenRead(fileName));
fileContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
{
FileName = fileName
};
content.Add(fileContent);
return client.PostAsync(requestUri, content).Result;
}
}
}
// UploadFileWithParam("http://example.com", @"c:'...", "param1", "value1").Dump();