在C#WebClient上使用Rest API PUT方法

时间:2018-07-10 09:05:57

标签: c# http upload webclient put

我如何使用Web客户端执行PUT方法?网络API应该上传一个CAD文件。使用Swagger UI可以正常工作。这是SwaggerUI的卷曲:

curl -X PUT "https://blablabla/test/api/v1/upload" -H  "accept: application/json" -H  "Authorization:xxxx" -H  "Content-Type: multipart/form-data" -d {"file":{},"filename":"jgv","path":"test_1/structure/test_2/test_3","partName":"hello"}

还有我的c#方法:

  try
        {
            string fileContent = string.Empty;
            using (StreamReader reader = new StreamReader(filePath))
            {
                fileContent = reader.ReadToEnd();
            }


            using (WebClient wc = new WebClient())
            {

                string uri = "https://pdengine-nsu.csi-entw.de/epm_livetest/api/v1/upload";

                wc.Headers["Authorization"] = sessionID;
                wc.Headers[HttpRequestHeader.ContentType] = "multipart/form-data"; 


                UploadDataModel model = new UploadDataModel
                {
                    file = fileContent,
                    filename = fileName,
                    partName = nameInStructure,
                    path = structurePath
                };

                var modelString = JsonConvert.SerializeObject(model)
                var result = wc.UploadString(uri, "PUT", modelString);

            }
        }
        catch (Exception e)
        {
            var error = e.Message;

        }

我已经尝试使用httpClient和UploadData在Json中进行一些更改。 我总是收到异常:(400)错误的请求...。

谢谢您的帮助

0 个答案:

没有答案