2
I created a very simple C# console application to test connection in an API, but I can’t submit the request. I tried with other Apis and they worked, but this particular one I can’t. It would be a query in the Cartolafc API (it is commented in the code below, the other works).
Following code: error generated:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace ConsoleAppHttp
{
class Program
{
static void Main(string[] args)
{
RunAsync();
}
static void RunAsync()
{
using (var clientAPI = new HttpClient())
{
//Um dos sites recomendava isso pra corrigir o problema, mas ainda não funciona
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11;
clientAPI.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//string sResponse = clientAPI.GetStringAsync("http://api.cartolafc.globo.com/mercado/status").Result; //Não Funciona
string sResponse = clientAPI.GetStringAsync("http://jsonplaceholder.typicode.com/posts").Result; //Funciona
Console.Write(sResponse);
}
Console.Read();
}
}
}
Follows error generated:
System.AggregateException ocorrido
HResult=0x80131500
Message=Um ou mais erros.
Source=mscorlib
StackTrace:
em System.Threading.Tasks.Task.ThrowIfExceptional(Boolean
includeTaskCanceledExceptions)
em System.Threading.Tasks.Task`1.GetResultCore(Boolean
waitCompletionNotification)
em System.Threading.Tasks.Task`1.get_Result()
em ConsoleAppHttp.Program.RunAsync() em e:\ricardo\documents\visual
studio 2017\Projects\ConsoleAppHttp\ConsoleAppHttp\Program.cs:linha 32
em ConsoleAppHttp.Program.Main(String[] args) em
e:\ricardo\documents\visual studio
2017\Projects\ConsoleAppHttp\ConsoleAppHttp\Program.cs:linha 21
Exceção interna 1:
HttpRequestException: Ocorreu um erro ao enviar a solicitação.
Exceção interna 2:
WebException: A conexão subjacente estava fechada: A conexão foi fechada de modo inesperado.
Does anyone have any idea how I can solve it? They can reproduce the problem?
Note: Using VS Community 2017, Windows 10 updated.
Thank you.
Yes, thank you. Soon after I posted here, they answered in another forum, the same answer. I have tested and it worked. I’m starting to mess with these connections, I’m an old-school programmer, I didn’t have any programming for a long time and I started "playing" again. Taking advantage, why was one API necessary and another not? How can I discover this need?
– R.Santos
I don’t know how to tell you right, but I’ve done some webcrawlers and I’ve been there, so before I answer you I did a test going to the browser and I made a get call, which means copy and paste it into the address bar and enter, and I saw that it worked, with that it could only be missing some parameter in your call and by the experience I set up only the Useragent and it worked, it could not have worked, then in this situation I would add other paramenters until it worked.
– Tiago S
Yes, that’s what I thought, I tried other parameters, but not the User-Agent.
– R.Santos