从控制台应用发出POST请求会导致错误403-访问被拒绝 [英] Making POST request from console app results in Error 403 - Access denied

查看:69
本文介绍了从控制台应用发出POST请求会导致错误403-访问被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用新的IBKR客户端门户API.我已经编写了一个简单的控制台程序来访问API,但是会导致错误403-访问被拒绝.如果我尝试来自邮递员的相同请求,它似乎正在工作.我尝试使用提琴手查看控制台应用程序发送的请求,但这会导致单独的错误.如何诊断此问题?

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 = 123456789.123456.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);
        }
    }
}

推荐答案

标头中缺少User-Agent. client.DefaultRequestHeaders.Add("User-Agent","Console");

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

这篇关于从控制台应用发出POST请求会导致错误403-访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆