将multiform cURL请求转换为C#post web request

时间:2014-10-24 08:20:17

标签: c# curl openstreetmap

我在

给出的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);

        }
    } 

1 个答案:

答案 0 :(得分:0)

如果你遇到类似的问题,

继承人如何在C#

中编写此特定请求

http://www.briangrinstead.com/blog/multipart-form-post-in-c

享受!