简体   繁体   中英

Rest client post request works in postman but not in c# code

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://ngb-uat.mpcz.in/mppkvvcl/nextgenbilling/backend/api/v1/authentication/login");
            //client.Proxy = new System.Net.WebProxy("proxy.mpcz.in:8080");

            var request = new RestRequest(Method.POST);
            request.AddHeader("Postman-Token", "e8a9e567-f7e6-4820-9096-a0aef2c2b003");
            request.AddHeader("cache-control", "no-cache");
            // request.AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36");
            request.AddHeader("Content-Type", "application/json");

            request.AddParameter("undefined", "{\n    \"username\": \"ADMIN_CZ\",\n    \"password\": \"ADMIN_CZ_1\"\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);
        }
    }
}

Ouput Headers:

***************x-content-type-options=nosniff ***************x-xss-protection=1; mode=block ***************pragma=no-cache ***************x-frame-options=DENY ***************Vary=accept-encoding ***************content-encoding= ***************transfer-encoding=chunked ***************connection=close ***************Cache-Control=no-cache, no-store, max-age=0, must-revalidate ***************Content-Type=application/json;charset=UTF-8 ***************Date=Tue, 26 Oct 2021 11:17:38 GMT ***************Expires=0 ***************X-Powered-By=Express InternalServerError

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM