首页 > 解决方案 > 用restsharp上传文件不会上传文件

问题描述

我在这里有一些 api 完整指南的 30 天免费试用帐户(第 24 页)(我们称之为“服务器”)。我正在尝试使用来自 NuGet dll 的 RestSharp上传文件。

问题是我总是从服务器得到文件哈希的响应,"md5": "d41d8cd98f00b204e9800998ecf8427e"这意味着服务器收到了一个空字符串。我知道问题出在我的代码中,因为我成功通过邮递员上传。

这是我的代码:

var path = @"C:\Users\daniel\Desktop\In\someFile.pdf";        
var client = new RestClient("https://te.checkpoint.com/tecloud/api/v1/file/upload");
var request = new RestRequest(Method.POST);    
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddHeader("Authorization", "6610F8512D02");
request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
request.AddFile("someFile.pdf", path);   
request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"someFile.pdf\"\r\nContent-Type: text/plain\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);

请分享你的知识

邮递员回复:

{
    "response": {
        "status": {
            "code": 1002,
            "label": "UPLOAD_SUCCESS",
            "message": "The file was uploaded successfully."
        },
        "md5": "d299af47bcb153189cbc7384bfae255f",
        "file_type": "",
        "file_name": "linktopdf.txt",
        "features": [
            "te"
        ],
        "te": {
            "trust": 0,
            "images": [
                {
                    "report": {
                        "verdict": "unknown"
                    },
                    "status": "not_found",
                    "id": "e50e99f3-5963-4573-af9e-e3f4750b55e2",
                    "revision": 1
                },
                {
                    "report": {
                        "verdict": "unknown"
                    },
                    "status": "not_found",
                    "id": "5e5de275-a103-4f67-b55b-47532918fa59",
                    "revision": 1
                }
            ],
            "score": -2147483648,
            "status": {
                "code": 1002,
                "label": "UPLOAD_SUCCESS",
                "message": "The file was uploaded successfully."
            }
        }


}

我的应用程序的响应:

{
  "response": {
    "status": {
      "code": 1001,
      "label": "FOUND",
      "message": "The request has been fully answered."
    },
    "md5": "d41d8cd98f00b204e9800998ecf8427e",
    "file_type": "",
    "file_name": "someFile.pdf",
    "features": [
      "te"
    ],
    "te": {
      "trust": 10,
      "images": [
        {
          "report": {
            "verdict": "benign"
          },
          "status": "found",
          "id": "e50e99f3-5963-4573-af9e-e3f4750b55e2",
          "revision": 1
        },
        {
          "report": {
            "verdict": "benign"
          },
          "status": "found",
          "id": "5e5de275-a103-4f67-b55b-47532918fa59",
          "revision": 1
        }
      ],
      "score": -2147483648,
      "combined_verdict": "benign",
      "status": {
        "code": 1001,
        "label": "FOUND",
        "message": "The request has been fully answered."
      }
    }
  }
}

标签: c#apirestsharp

解决方案


推荐阅读