0
I’m doing a post for a third party API but would like to get the return code from it to save in our bank, however, I can only get this return code if the code is 202, if it was 401 or 500 it returns me null, i need to return any return code and not null.
public static RetornoRequisicao HttpRequestJson(string token, string url, string dados)
{
WebRequest request = WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json";
request.Headers.Add("Authorization", token);
string retorno;
int status;
try
{
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
string json = dados;
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
var httpResponse = (HttpWebResponse)request.GetResponse();
status = (int)httpResponse.StatusCode;
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
retorno = streamReader.ReadToEnd();
}
}
return new RetornoRequisicao
{
retorno = retorno,
status = status
};
}
catch (Exception e)
{
return null;
}
}
this would be a "good" alternative but would be gambiarrada, my intention is to bring really the status code and not a random code, I got null with code 500, but could give 404, 401 and etc..
– Matheus Muniz
I made a code that returns a random status because you specified in your question: i need to return any return code and not null.
– CypherPotato