WebAPI 当 运行 使用 Postman 的 PUT 方法正常时,运行 API 控制器将失败

WebAPI when running PUT method with Postman is fine, running with API controller will fail

我遇到了很大的麻烦。我不明白为什么。当我在我的网络服务上执行 PUT 方法时,系统 returns “200 ok”,但是当按 API 执行时,我得到“401 unauthorized”错误。这是我的 API 代码。

  using (var client = new HttpClient())
    {
        //string datetime = DateTime.Now.ToString("yyyy-MM-dd");
        var request = new HttpRequestMessage(HttpMethod.Put, "https://....../api/books/" + GetIdUser(cid));// dan ma code vao day getcard
        request.Headers.Add("ContentType", "application/json");
        request.Headers.Add("Authorization", "Bearer FO_KO7XTNe6tam...FG7L5gbGObl4z");
        var response = await client.SendAsync(request);
        var a = await response.Content.ReadAsStringAsync();// chuoi gia tri can gan vao trong book
        var content = JsonConvert.DeserializeObject<PUT2Bien.Book>(a);

        //string datetime1 = DateTime.Now.ToString("yyyy-MM-dd");
        var request1 = new HttpRequestMessage(HttpMethod.Put, "https://......../api/allocations/5eaa75c18d64e05fde5fd540");
        request1.Headers.Add("ContentType", "application/json");
        request1.Headers.Add("Authorization", "Bearer FO_KO7XTNe6tam...FG7L5gbGObl4z");
        var response1 = await client.SendAsync(request1);
        string b = await response1.Content.ReadAsStringAsync();// chuoi gia tri can gan vao trong book

        try
        {
            var content1 = JsonConvert.DeserializeObject<PUT2Bien.Root>(b);
            for (int i = 0; i < content1.seats.Count; i++)
            {
                if (content1.seats[i]._id == "5eb3c344a9e9486ebb3450cd")//moi may tinh cai dat se co 1 id khac nhau//
                {
                    if (content1.seats[i].book == null)
                    {
                        content1.seats[i].book = content;
                    }
                }
            }
            var json = JsonConvert.SerializeObject(content1,Formatting.Indented);
            var httpContent = new StringContent(json, Encoding.UTF8, "application/json");
            var response3 = await client.PutAsync("https://......./api/allocations/5eaa75c18d64e05fde5fd540", httpContent);
            MessageBox.Show("" + response3.StatusCode);
        }

{
  "_id": "5eaa75c18d64e05fde5fd540",
  "account": "demo",
  "name": "院内",
  "image": "https://s3-ap-northeast-1.amazonaws.com/..../images/demo/allocations/eeb38e0a-3858-4353-b2c3-fbec598d1814.jpg",
  "seats": [
    {
      "cleaning": false,
      "_id": "5eb3c344a9e9486ebb3450cd",
      "left": 0.36587301587301585,
      "top": 0.13202256244963739,
      "width": 0.066666666666666707,
      "height": 0.070136986301369858,
      "book": {
        "users": [],
        "tags": [],
        "note": "",
        "_id": "5f9ca83e1249971c625a7981",
        "line": "5c51541c93908265a7668caf",
        "start": "2020-10-31T00:50:00Z",
        "end": "2020-10-31T03:30:00Z",
        "type": "appointment",
        "status": "waiting",
        "card": {
          "recall": {
            "tags": [],
            "users": [],
            "date": "2020-10-20T15:00:00Z"
          },
          "gender": "male",
          "risk": 0,
          "tags": [
            {
              "effect": "blink",
              "_id": "5bfa0966b13b093268c0235c",
              "name": "SET",
              "color": "#ffa000",
              "account": "demo",
              "icon": "https://s3-ap-northeast-1.amazonaws.com/..../images/demo/tags/f2d0eb03-2996-48f6-90d0-5aec98912059.png"
            }
          ],
          "users": [
            {
              "admin": false,
              "_id": "5a55d0ed0ddfaa11476df01a",
              "updatedAt": "2018-01-10T08:38:05.622Z",
              "createdAt": "2018-01-10T08:38:05.622Z",
              "name": "高田",
              "color": "#ff9800",
              "username": "",
              "account": "demo",
              "group": "DH",
              "hidden": false,
              "kana": "たかだ",
              "dentistCode": "102",
              "nopass": true
            }
          ],
          "relations": [],
          "_id": "5a55d12315e0200f04ec1e90",
          "account": "demo",
          "cid": "3004",
          "createdAt": "2018-01-10T08:38:59.924Z",
          "updatedAt": "2020-10-21T02:39:17.022Z",
          "name": "波多野 亮",
          "kana": "ハタノ リョウ",
          "email": "hatano_ryou@example.com",
          "birthday": "1952/12/12",
          "address": "大阪府",
          "phone": "090-982-2851"
        },
        "account": "demo",
        "createdAt": "2020-10-30T23:56:46.068Z",
        "updatedAt": "2020-10-31T07:24:09.494Z",
        "__v": 42,
        "logs": {
          "waiting": "2020-10-31T03:15:24.184Z"
        },
        "startedAt": "2020-10-31T00:47:58.354Z"
      }
    },
    {
      "cleaning": false,
      "_id": "5eb3c352a9e9486ebb3450d3",
      "left": 0.46825396825396826,
      "top": 0.13477303250067149,
      "width": 0.066666666666666652,
      "height": 0.064636046199301661,
      "book": null
    },
    {
      "cleaning": false,
      "_id": "5eb3c356a9e9486ebb3450d5",
      "left": 0.6253968253968254,
      "top": 0.33693258125167874,
      "width": 0.049206349206349254,
      "height": 0.10314262691377923,
      "book": null
    },
    {
      "cleaning": false,
      "_id": "5eb3c35aa9e9486ebb3450d8",
      "left": 0.36507936507936506,
      "top": 0.55146924523233953,
      "width": 0.063492063492063489,
      "height": 0.07701316142895509,
      "book": null
    },
    {
      "cleaning": false,
      "_id": "5eb3c360a9e9486ebb3450dc",
      "left": 0.46904761904761905,
      "top": 0.55146924523233953,
      "width": 0.063492063492063489,
      "height": 0.07701316142895509,
      "book": null
    },
    {
      "cleaning": false,
      "_id": "5eb3c365a9e9486ebb3450e1",
      "left": 0.62777777777777777,
      "top": 0.76188020413644908,
      "width": 0.046825396825396881,
      "height": 0.10451786193929624,
      "book": null
    }
  ],
  "createdAt": "2020-04-30T06:52:49.461Z",
  "updatedAt": "2020-10-31T07:24:09.585Z"
}

谁能帮帮我,万分感谢!

您必须添加授权header:

var httpContent = new StringContent(json, Encoding.UTF8, "application/json");
client.DefaultRequestHeaders.Authorization 
                         = new AuthenticationHeaderValue("Bearer", "Your Oauth token");
var response3 = await client.PutAsync("https://......./api/allocations/5eaa75c18d64e05fde5fd540", httpContent);

您是根据请求将其添加到所有其他三种情况,但不是最后一种。

如果您对所有调用使用相同的 Bearer,则可以在函数开始时执行一次,无需在每个不同的请求上添加它。