【问题标题】:Rest client post request works in postman but not in c# code休息客户端发布请求在邮递员中有效,但在 C# 代码中无效
【发布时间】:2021-12-11 18:32:36
【问题描述】:
using System; 
using System.Collections.Generic; 
using System.Linq;
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using RestSharp; 
using System.Diagnostics;

namespace mobile_link_account 
{
    public partial class update_mobile_link_ngb : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
            var client = new RestClient("https://xyz.apcz.in/appkvvcl/ngb/backend/api/v1/authentication/login");
           

            var request = new RestRequest(Method.POST);
            request.AddHeader("Postman-Token", "e8a9e567-f7e6-4820-9096-a0aef2c2b003");
            request.AddHeader("cache-control", "no-cache");
          
            request.AddHeader("Content-Type", "application/json");

            request.AddParameter("undefined", "{\n    \"username\": \"xyz\",\n    \"password\": \"xys\"\n}",
ParameterType.RequestBody);

            IRestResponse response = client.Execute(request);

            for (int i = 0; i < response.Headers.Count; i++)
            {
                Debug.WriteLine("***************" + response.Headers[i]);
            }

            Debug.WriteLine("" + response.StatusCode);
        }
    }
}

输出标题:

***************x-content-type-options=nosniff ******************x-xss-保护=1;模式=块 ****************** pragma=no-cache ******************x-frame-options=拒绝 ***************Vary=接受编码 ***************内容编码= ******************传输编码=分块 ******************连接=关闭 ******************Cache-Control=no-cache, no-store, max-age=0, must-revalidate ******************内容类型=应用程序/json;字符集=UTF-8 ***************日期=格林威治标准时间 2021 年 10 月 26 日星期二 11:17:38 ******************过期=0 ******************X-Powered-By=Express InternalServerError

【问题讨论】:

  • 是的.....和!?!?发生什么了?您是否收到错误 - 如果是:what 错误?请发布有关您的情况的更多背景信息和信息 - 照原样,没有人可以帮助您!
  • 我收到错误消息“内部服务器错误”,但在邮递员上测试时,post 方法工作正常

标签: c# asp.net-web-api restsharp


【解决方案1】:

如果是内部服务器错误,即 500,则使用断点调试该方法并告诉我们会发生什么

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-01
    • 2021-01-14
    • 1970-01-01
    • 2018-03-29
    相关资源
    最近更新 更多