C#控制台Rest API标头有效负载

C#控制台Rest API标头有效负载,c#,rest,api,C#,Rest,Api,我需要访问一个由承载身份验证控制的门户。客户端需要获取身份验证令牌,然后将其添加到每个请求中 URL~/token 路标 内容类型应用程序/x-www-form-urlencoded 有效负载授权类型=密码和用户名=用户名和密码=密码 **如何将包含授权类型、用户名和密码的有效负载添加到代码中** 到目前为止,我的代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Text;

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

URL~/token 路标 内容类型应用程序/x-www-form-urlencoded 有效负载授权类型=密码和用户名=用户名和密码=密码

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

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

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";

       }
     }`
   }

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

  • 标题:

    内容类型:application/json

  • 正文:

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


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

  • 标题:

    内容类型:application/json

  • 正文:

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


您可能可以尝试添加请求头,方法如下:

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>);
stringuri=”https://token";
WebRequest=WebRequest.Create(URI);
request.Method=“POST”;
request.ContentType=“application/x-www-form-urlencoded”;
request.headers.Add(,);

然后,您可以在api上阅读这些标题。

您可能可以尝试通过以下操作添加到请求标题中:

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>);
stringuri=”https://token";
WebRequest=WebRequest.Create(URI);
request.Method=“POST”;
request.ContentType=“application/x-www-form-urlencoded”;
request.headers.Add(,);
然后在api上阅读这些标题