【问题标题】:C# Console Rest API header payloadC# 控制台 Rest API 标头有效负载
【发布时间】:2018-01-26 21:32:57
【问题描述】:

我需要访问一个由承载身份验证控制的门户。客户端需要获取身份验证令牌,然后将其添加到每个请求中

网址 ~/token 方法 POST 内容类型应用程序/x-www-form-urlencoded 有效载荷 grant_type=password&username=UserName&password=Password

**如何将包含授权类型、用户名和密码的有效负载添加到我的代码中**

到目前为止,我的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;

 namespace ConsoleRestClient
 {
     class Program
    {
    static void Main(string[] args)
    {
        string URI = "https://token";
        WebRequest request = WebRequest.Create(URI);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";

       }
     }`
   }

【问题讨论】:

    标签: c# rest api


    【解决方案1】:

    您应该发送内容类型为“application/json”的请求,并将请求的登录变量放在正文中。给你一个想法:

    • 标题:

      内容类型:应用程序/json

    • 主体:

      {“用户名”:“我的用户名”,“密码”:“我的密码”}

    【讨论】:

      【解决方案2】:

      也许您可以尝试添加到请求标头中,方法如下:

      string URI = "https://token";
      WebRequest request = WebRequest.Create(URI);
      request.Method = "POST";
      request.ContentType = "application/x-www-form-urlencoded";
      request.headers.Add(<header_name>, <header_value>);
      

      然后你在你的 api 上阅读这些标题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-06
        • 2019-04-03
        • 2017-06-22
        • 2021-09-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-30
        相关资源
        最近更新 更多