internal Tuple <HttpStatusCode, string> MakeRequest(string path, HttpMethod method, HTTPContentType contentType = HTTPContentType.json, byte[] body = null, bool isQRS = true, string port = "4242") { // Fix Path if (!path.StartsWith("/")) { path = '/' + path; } if (path.EndsWith("/")) { path = path.Substring(0, path.Length - 1); } int indexOfSlash = path.LastIndexOf('/'); int indexOfQuery = path.LastIndexOf('?'); if (indexOfQuery <= indexOfSlash) { path += "?"; } else { path += "&"; } string responseString = ""; HttpStatusCode responseCode = 0; string xrfkey = "0123456789abcdef"; ServicePointManager.ServerCertificateValidationCallback = delegate { return(true); }; ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11; HttpRequestMessage req = new HttpRequestMessage(); if (isQRS) { req.RequestUri = new Uri(@"https://" + _hostname + ":4242/qrs" + path + "xrfkey=" + xrfkey); } else { req.RequestUri = new Uri(@"https://" + _hostname + ":" + port + path); } req.Method = method; if (isQRS) { req.Headers.Add("X-Qlik-xrfkey", xrfkey); req.Headers.Add("X-Qlik-User", @"UserDirectory=internal;UserId=sa_api"); } WebRequestHandler handler = new WebRequestHandler(); handler.ClientCertificates.Add(_certificate); if (method == HttpMethod.Post || method == HttpMethod.Put) { req.Content = new ByteArrayContent(body); // Set Headers if (contentType == HTTPContentType.json) { req.Content.Headers.Remove("Content-Type"); req.Content.Headers.Add("Content-Type", "application/json"); } else if (contentType == HTTPContentType.app) { req.Content.Headers.Remove("Content-Type"); req.Content.Headers.Add("Content-Type", "application/vnd.qlik.sense.app"); } else { throw new ArgumentException("Content type '" + contentType.ToString() + "' is not supported."); } } TelemetryDashboardMain.Logger.Log(string.Format("Request - Ready to send request: '{0}' to '{1}'.", method.ToString(), path), LogLevel.Debug); using (HttpClient client = new HttpClient(handler)) { client.Timeout = TimeSpan.FromMilliseconds(_timeout); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); try { Task <HttpResponseMessage> responseTask = client.SendAsync(req); responseTask.Wait(); responseTask.Result.EnsureSuccessStatusCode(); responseCode = responseTask.Result.StatusCode; Task <string> responseBodyTask = responseTask.Result.Content.ReadAsStringAsync(); responseBodyTask.Wait(); responseString = responseBodyTask.Result; } catch (Exception e) { if (responseCode != 0) { return(new Tuple <HttpStatusCode, string>(responseCode, e.Message)); } else { return(new Tuple <HttpStatusCode, string>(HttpStatusCode.InternalServerError, e.Message)); } } } TelemetryDashboardMain.Logger.Log(string.Format("Request - Response code received: '{0}'", responseCode), LogLevel.Debug); return(new Tuple <HttpStatusCode, string>(responseCode, responseString)); }
private static Tuple <HttpStatusCode, string> MakeQrsRequest(string path, HTTPMethod method, HTTPContentType contentType = HTTPContentType.json, byte[] body = null) { // Fix Path if (!path.StartsWith("/")) { path = '/' + path; } if (path.EndsWith("/")) { path = path.Substring(0, path.Length - 1); } int indexOfSlash = path.LastIndexOf('/'); int indexOfQuery = path.LastIndexOf('?'); if (indexOfQuery <= indexOfSlash) { path += "?"; } else { path += "&"; } string responseString = ""; HttpStatusCode responseCode = 0; ServicePointManager.ServerCertificateValidationCallback = delegate { return(true); }; //Create the HTTP Request and add required headers and content in xrfkey string xrfkey = "0123456789abcdef"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"https://*****:*****@"UserDirectory=internal;UserId=sa_api"); // Add the certificate to the request and provide the user to execute as request.ClientCertificates.Add(SENSE_CERT.Value); if (method == HTTPMethod.POST || method == HTTPMethod.PUT) { // Set Headers if (contentType == HTTPContentType.json) { request.ContentType = "application/json"; } else if (contentType == HTTPContentType.app) { request.ContentType = "application/vnd.qlik.sense.app"; } else { throw new ArgumentException("Content type '" + contentType.ToString() + "' is not supported."); } // Set Body if (body == null) { request.ContentLength = 0; } else { request.ContentLength = body.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(body, 0, body.Length); requestStream.Close(); } } // make the web request and return the content try { using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { responseCode = response.StatusCode; using (Stream stream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(stream)) { // if response is invalid, throw exception and expect it to be handled in calling function (4xx errors) responseString = JsonConvert.SerializeObject(reader.ReadToEnd()); } } } } catch (WebException webEx) { return(new Tuple <HttpStatusCode, string>(HttpStatusCode.ServiceUnavailable, webEx.Message)); } catch (Exception e) { return(new Tuple <HttpStatusCode, string>(HttpStatusCode.InternalServerError, e.Message)); } return(new Tuple <HttpStatusCode, string>(responseCode, responseString)); }
private static Tuple <HttpStatusCode, string> MakeQrsRequest(string path, HttpMethod method, HTTPContentType contentType = HTTPContentType.json, byte[] body = null) { // Fix Path if (!path.StartsWith("/")) { path = '/' + path; } if (path.EndsWith("/")) { path = path.Substring(0, path.Length - 1); } int indexOfSlash = path.LastIndexOf('/'); int indexOfQuery = path.LastIndexOf('?'); if (indexOfQuery <= indexOfSlash) { path += "?"; } else { path += "&"; } string responseString = ""; HttpStatusCode responseCode = 0; string xrfkey = "0123456789abcdef"; ServicePointManager.ServerCertificateValidationCallback = delegate { return(true); }; ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11; HttpRequestMessage req = new HttpRequestMessage(); req.RequestUri = new Uri(@"https://" + HOSTNAME.Value + ":4242/qrs" + path + "xrfkey=" + xrfkey); req.Method = method; req.Headers.Add("X-Qlik-xrfkey", xrfkey); req.Headers.Add("X-Qlik-User", @"UserDirectory=internal;UserId=sa_api"); WebRequestHandler handler = new WebRequestHandler(); handler.ClientCertificates.Add(SENSE_CERT.Value); if (method == HttpMethod.Post || method == HttpMethod.Put) { req.Content = new ByteArrayContent(body); // Set Headers if (contentType == HTTPContentType.json) { req.Content.Headers.Remove("Content-Type"); req.Content.Headers.Add("Content-Type", "application/json"); } else if (contentType == HTTPContentType.app) { req.Content.Headers.Remove("Content-Type"); req.Content.Headers.Add("Content-Type", "application/vnd.qlik.sense.app"); } else { throw new ArgumentException("Content type '" + contentType.ToString() + "' is not supported."); } } using (HttpClient client = new HttpClient(handler)) { client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); try { Task <HttpResponseMessage> responseTask = client.SendAsync(req); responseTask.Wait(); responseTask.Result.EnsureSuccessStatusCode(); responseCode = responseTask.Result.StatusCode; Task <string> responseBodyTask = responseTask.Result.Content.ReadAsStringAsync(); responseBodyTask.Wait(); responseString = responseBodyTask.Result; } catch (Exception e) { if (responseCode != 0) { return(new Tuple <HttpStatusCode, string>(responseCode, e.Message)); } else { return(new Tuple <HttpStatusCode, string>(HttpStatusCode.InternalServerError, e.Message)); } } } return(new Tuple <HttpStatusCode, string>(responseCode, responseString)); }