Getting error Unable to connect to the remote server when used httpclient c#

Hello,

I am calling Deal api below api request using the HTTPclient request.

https://api.hubapi.com/deals/v1/deal/42323232

using (var client = new HttpClient())

{

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(“Bearer”, “tocken”);

client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(“application/json”));

url = “https://api.hubapi.com/” + “deals/v1/deal/” + objdata.hs_object_id.ToString();

response = client.GetAsync(url).Result; // getting error

if (response.IsSuccessStatusCode)

{

}

}
error details :

A connection attempt failed because the connected party did not properly respond after a period of time,
or established connection failed because connected host has failed to respond 172.16.3.182:1251

Hey @KMevada,

Can you try this code and see if you are getting a response?

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get, "https://api.hubapi.com/deals/v1/deal/YOU_DEAL_ID");
request.Headers.Add("Authorization", "Bearer YOUR_TOKEN");
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
Console.WriteLine(await response.Content.ReadAsStringAsync());

Also, I’d strongly suggest using the newer v3 CRM endpoints here. From your request it appears you are using the legacy v1 endpoint here. Same request with updated endpoint below:

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get, "https://api.hubapi.com/crm/v3/objects/deals/YOUR_DEAL_ID");
request.Headers.Add("Authorization", "Bearer YOUR_TOKEN");
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
Console.WriteLine(await response.Content.ReadAsStringAsync());

I’m not able to reproduce what you are seeing as the above is working for me successfully so I’m not sure if the issue lies on the HubSpot side. It could potentially be a conflict within your code base?

Hi @coldrickjack ,

Thank you so much for replying.

I check both url v1 and v3 and same code I used which you are provided. But still I am getting same error.

When i used same details using Postman, it’s working fine. But when i execute this in my local code debug mode. it’s returning error.