将多种形式的cURL请求转换为C#post web请求
本文关键字:请求 C#post web 转换 cURL 多种形式 | 更新日期: 2023-09-27 18:27:37
在Curl中给出了这个多部分表单POST请求
curl -u user:pass -H "Expect: " -F "file=@"C:'Users'Desktop'45.gpx -F description=test -F tags=xxxx -F visibility=xxxx http://www.openstreetmap/api/0.6/gpx/create
我一直试图将其转换为C#,但不知怎么的,我一直收到一个错误的请求400。
System.Collections.Specialized.NameValueCollection reqparm = new System.Collections.Specialized.NameValueCollection();
reqparm.Add("description", "test");
reqparm.Add("tags", "upload");
reqparm.Add("visibility", "public");
reqparm.Add("file", fileName);
using (WebClient client = new WebClient())
{
try
{
client.Credentials = new NetworkCredential("user", "pass");
//client.Headers.Add("description", "test");
//client.Headers.Add("tags", "upload");
//client.Headers.Add("visibility", "public");
//client.Headers.Add("file", fileName);
byte[] responsebytes = client.UploadValues(url, "POST",reqparm);
string responsebody = Encoding.UTF8.GetString(responsebytes);
Console.WriteLine(responsebody);
}
catch (WebException ex)
{
Console.WriteLine(ex.Message);
}
}
如果你遇到类似的问题,
以下是如何在C#中编写此特定请求
http://www.briangrinstead.com/blog/multipart-form-post-in-c
享受吧!