07-13-2022 03:41 AM
I'm attempting to set up a connection to Stealthwatch via an SSIS c# script. The following fails on the last line with message "The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel." The curl command in the documentation has a "-K" option which might be the issue, but I don't know how that needs to be reflected in my json request.
string wUrl = "https://stealthwatch.nas.hscn.nhs.uk/token/v2/authenticate";
var httpRequest = (HttpWebRequest)WebRequest.Create(wUrl);
httpRequest.Method = "POST";
httpRequest.ContentType = "application/json";
var data = "{\"username\":\"gsheehan\",\"password\":\"****\"}";
using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream()))
{
streamWriter.Write(data);
}
var httpResponse = (HttpWebResponse)httpRequest.GetResponse();
Solved! Go to Solution.
07-22-2022 08:42 AM
Looks like I had an issue with how I passed in the credentials. The following worked:
// Build Username/password in to POST data
var postData = "username=" + Uri.EscapeDataString(username);
postData += "&password=" + Uri.EscapeDataString(password);
var data = Encoding.ASCII.GetBytes(postData);
httpRequest.Method = "POST";
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.ContentLength = data.Length;
using (var streamWriter = httpRequest.GetRequestStream())
{
streamWriter.Write(data, 0, data.Length);
}
Your help was much appreciated. I'll close the case
07-13-2022 07:18 AM
You need to bypass the certificate check (or ignore any ssl related errors). not too sure about C# but according to google this should work:
string wUrl = "https://stealthwatch.nas.hscn.nhs.uk/token/v2/authenticate";
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
var httpRequest = (HttpWebRequest)WebRequest.Create(wUrl);
httpRequest.Method = "POST";
httpRequest.ContentType = "application/json";
var data = "{\"username\":\"gsheehan\",\"password\":\"****\"}";
using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream()))
{
streamWriter.Write(data);
}
var httpResponse = (HttpWebResponse)httpRequest.GetResponse();
HTH
Marcel
07-22-2022 04:35 AM
Thanks Marcel. It made a difference, in that I receive a different error now : "The remote server returned an error: (415) Unsupported Media Type."
I added extra code:
httpRequest.MediaType = "application/json";
httpRequest.Accept = "application/json";
but it hasn't made any difference.
07-22-2022 08:42 AM
Looks like I had an issue with how I passed in the credentials. The following worked:
// Build Username/password in to POST data
var postData = "username=" + Uri.EscapeDataString(username);
postData += "&password=" + Uri.EscapeDataString(password);
var data = Encoding.ASCII.GetBytes(postData);
httpRequest.Method = "POST";
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.ContentLength = data.Length;
using (var streamWriter = httpRequest.GetRequestStream())
{
streamWriter.Write(data, 0, data.Length);
}
Your help was much appreciated. I'll close the case
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide