A
I am building a desktop application using C#, .NET8.0. I want to pull historic price values . I am getting error 'Raw Response Content: Request Rejected. The requested URL was rejected. Please consult with your administrator. Your support ID is 5492786208459174548' for the below code. Please let me know how to proceed.
Exchange = NSE
AssetName = Tata Motors
public static decimal? GetCurrentPrice(string exchange, string assetName)
{
string? accessToken = GetAccessToken();
if (accessToken == null)
{
MessageBox.Show("Failed to retrieve access token.");
return null;
}
try
{
var client = new RestClient("https://apiconnect.angelone.in/rest/secure/angelbroking/historical/v1/getCandleData");
var request = new RestRequest
{
Method = Method.Get
};
request.AddHeader("Authorization", $"Bearer {accessToken}");
request.AddHeader("X-API-KEY", apiKey);
// Add query parameters for the historical data request
request.AddParameter("exchange", exchange);
request.AddParameter("symbol", assetName);
request.AddParameter("interval", "ONE_DAY");
request.AddParameter("fromdate", DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"));
request.AddParameter("todate", DateTime.Now.ToString("yyyy-MM-dd"));
RestResponse response = client.Execute(request);
// Check if headers are not null and display them
var headers = response.Headers != null
? string.Join(Environment.NewLine, response.Headers.Select(h => $"{h.Name}: {h.Value}"))
: "No headers available";
MessageBox.Show($"Response Status: {response.StatusCode}\nResponse Headers:\n{headers}", "API Response", MessageBoxButton.OK, MessageBoxImage.Information);
if (response.IsSuccessful)
{
if (string.IsNullOrEmpty(response.Content))
{
MessageBox.Show("Empty response content. The market might be closed or data is unavailable.");
return null;
}
// Print the raw response for debugging
MessageBox.Show($"Raw Response Content: {response.Content}");
try
{
var priceData = Newtonsoft.Json.Linq.JObject.Parse(response.Content);
var currentPrice = priceData["data"]?[0]?["close"]?.Value<decimal>();
return currentPrice;
}
catch (Newtonsoft.Json.JsonException jsonEx)
{
MessageBox.Show($"JSON Parsing Error: {jsonEx.Message}", "JSON Parsing Error", MessageBoxButton.OK, MessageBoxImage.Error);
return null;
}
}
else
{
MessageBox.Show($"Error: {response.StatusCode} - {response.Content}", "API Error", MessageBoxButton.OK, MessageBoxImage.Error);
return null;
}
}
catch (Exception ex)
{
MessageBox.Show($"Exception occurred: {ex.Message}", "Exception", MessageBoxButton.OK, MessageBoxImage.Error);
return null;
}
}