Kyle Abrahams, PMP
asked on
Rest request fails first time but works on caught exception
Hi All,
I'm calling the code below.
using (Stream stream = request.GetRequestStream() ) throws an error the first time but when I call authenticate again from within the catch it works.
Postman using the same variables works first time. I've engaged the hosting company but trying to determine if there's an issue on my side or a reason why it would fail the first time from my code.
Thanks in advanced for the help.
I'm calling the code below.
using (Stream stream = request.GetRequestStream()
Postman using the same variables works first time. I've engaged the hosting company but trying to determine if there's an issue on my side or a reason why it would fail the first time from my code.
Thanks in advanced for the help.
private static Authenticate Authenticate(bool secondTry=false)
{
string url = Config.URL + "token";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.KeepAlive = false;
request.Headers.Add("Authorization", "Basic " + Utility.Base64Encode(string.Format("{0}:{1}", Config.ConsumerKey, Config.SecretKey)));
request.Method = "POST";
//what I'm sending to the web request on a post.
string postData = "grant_type=STUFF";
Byte[] data = Encoding.UTF8.GetBytes(postData);
request.ContentLength = data.Length;
// default settings.
request.Accept = "application/json";
request.ContentType = "application/x-www-form-urlencoded";
AttachClientCert(ref request); // Attaches PFX to request
setTLS(); //set TLS to 1.2 and other ServiceManager configs.
try
{
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
}
catch (Exception ex)
{
// first time thru FAILS -> Authentication failed because the remote party has closed the transport stream
if (!secondTry)
{
return Authenticate(true); // now suddenly works.
}
}
WebResponse response = (HttpWebResponse)request.GetResponse();
string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
Authenticate auth = JsonConvert.DeserializeObject<ResponseAuthenticate>(responseString).FromAuthenticateResponse();
return auth;
}
private static void setTLS()
{
//use TLS 1.2, Ignore cert errors.
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;//| (SecurityProtocolType)768 ;
ServicePointManager.ServerCertificateValidationCallback +=
delegate (object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
System.Security.Cryptography.X509Certificates.X509Chain chain,
System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true; // **** Always accept
};
}
private static void AttachClientCert(ref HttpWebRequest request)
{
// X509Certificate2Collection certificates = new X509Certificate2Collection();
X509Certificate2 cert = new X509Certificate2(File.ReadAllBytes(Config.Certificate), "PASSWORD", X509KeyStorageFlags.PersistKeySet);
request.ClientCertificates.Add(cert);
//certificates.Import(Config.Certificate, "PASSWORD", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);
//request.ClientCertificates = certificates;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Yep
It just helped me :-)
Thanks for the tip.
It just helped me :-)
Thanks for the tip.
ASKER