繁体   English   中英

将多格式cURL请求转换为C#发布Web请求

[英]Convert multiform cURL request to C# post web request

我在由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

请享用!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM