Making POST request from console app results in Error 403 - Access denied

戏子无情 提交于 2021-01-28 20:02:12

问题


I am trying to use the new IBKR Client Portal API. I have written a simple console program to access the API but it results in Error 403 - Access denied. If I try the same request from Postman it seems to be working. I tried using fiddler to see the request that console app sends but that results in a separate error. How can I diagnose this issue?

using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static async Task Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            var baseURL = "https://localhost:5000/v1/portal";
            HttpClientHandler handler = new HttpClientHandler();
            handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
            var client = new HttpClient(handler);
            client.DefaultRequestHeaders.Add("Cookie", "ibkr.il = 571738634.47873.0000");
            client.DefaultRequestHeaders.Add("AcceptEncoding", "gzip, deflate, br");
            var response = await client.GetAsync(baseURL + "/sso/validate");
            string result = response.Content.ReadAsStringAsync().Result;
            Console.WriteLine(result);
        }
    }
}

回答1:


It was missing User-Agent from the header. client.DefaultRequestHeaders.Add("User-Agent", "Console");



来源:https://stackoverflow.com/questions/64597071/making-post-request-from-console-app-results-in-error-403-access-denied

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!