The request was aborted: The connection was closed unexpectedly.

mohammedmullick
Member

Hi,

 

I am doing a PUT request using Postman and it is successfully returning a Status Code 400 and a message, but when I run below C# code (which I copied from Postman), I get Status Code 0 and error message "The request was aborted: The connection was closed unexpectedly.". Please can someone advise what could be the issue?

 

Environment: Visual Studio 2015

.NET Framework: 4.6.1

Code:

 

var client = new RestClient("https://api.hubapi.com/companies/v2/companies/XXXXXXXX?hapikey=XXXXXXXXXXXXXXXXXXXXXXXXXX");
client.Timeout = -1;
var request = new RestRequest(Method.PUT);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\r\n \"properties\": [\r\n {\r\n \"name\": \"cardcode\",\r\n \"value\": \"Z064122\"\r\n },\r\n {\r\n \"name\": \"name\",\r\n \"value\": \"XXXXXX\"\r\n },\r\n {\r\n \"name\": \"groupcode\",\r\n \"value\": \"Prospect\"\r\n },\r\n {\r\n \"name\": \"currency\",\r\n \"value\": \"USD\"\r\n },\r\n {\r\n \"name\": \"database_company\",\r\n \"value\": \"XXXXXXXXX\"\r\n },\r\n {\r\n \"name\": \"push_to_crm_company_\",\r\n \"value\": \"Yes\"\r\n },\r\n {\r\n \"name\": \"turnover\",\r\n \"value\": \"0 - 4.99\"\r\n },\r\n {\r\n \"name\": \"bp_type\",\r\n \"value\": \"Lead\"\r\n },\r\n {\r\n \"name\": \"u_hosting\",\r\n \"value\": \"-\"\r\n },\r\n {\r\n \"name\": \"u_contractvalue\",\r\n \"value\": \"0.000000\"\r\n },\r\n {\r\n \"name\": \"u_contractrdate\",\r\n \"value\": \"0\"\r\n },\r\n {\r\n \"name\": \"hubspot_owner_id\",\r\n \"value\": \"XXXXXXXXXX\"\r\n },\r\n {\r\n \"name\": \"core_product\",\r\n \"value\": \"XXXXX\"\r\n },\r\n {\r\n \"name\": \"company_telephone_1\",\r\n \"value\": \"XXXXXXXXX\"\r\n },\r\n {\r\n \"name\": \"county_us\",\r\n \"value\": \"California\"\r\n },\r\n {\r\n \"name\": \"zip\",\r\n \"value\": \"XXXXXXXX\"\r\n },\r\n {\r\n \"name\": \"city\",\r\n \"value\": \"XXXXXXXXXX\"\r\n },\r\n {\r\n \"name\": \"country_crm\",\r\n \"value\": \"US\"\r\n },\r\n {\r\n \"name\": \"company_bde_owner\",\r\n \"value\": \"XXXXXXXXXXX\"\r\n }\r\n ]\r\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);

 

 

Note: I already tried adding below code, but still no success.

 

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

0 Upvotes
4 Replies 4
WendyGoh
HubSpot Moderator

Hey @mohammedmullick,

 

Digging into this error, I was able to locate this stackoverflow thread: Restsharp - The request was aborted: The connection was closed unexpectedly - Stack Overflow and you might want to try setting the ConnectionGroupName on IRestClient nuget.org/packages/RestSharp/106.2.0-alpha0032 and see if it helps.

0 Upvotes
mohammedmullick
Member

Hi Wendy,

 

Thank you for your response. 

Do you know what should be the value for ConnectionGroupName? I can see it's a string type but don't know what value to use.

Sorry, I could not figure it out.

 

Many thanks.

0 Upvotes
WendyGoh
HubSpot Moderator

Hey @mohammedmullick,

 

Based on this github add connection groupname by cembasaranoglu · Pull Request #1027 · restsharp/RestSharp · GitHub and this article HttpWebRequest reuse of TCP Connections – All Things Cloud, I believe the connection group name is unique for each request and you can set it based on the http request:

 

http.ConnectionGroupName = ConnectionGroupName;

 

0 Upvotes
mohammedmullick
Member

Hey @WendyGoh 

None of the examples provide a definition for ConnectionGroupName value so I used a random text value "GRP1", but I am still getting the same error message.

0 Upvotes