I am here having an issue with the HttpsClient.
The request runs into an "unknown error" whenever I try to add Content-Length to the header. It runs fine otherwise.
About the sample code below: you can directly copy/paste into VS2008 and load it as a test. I added a console command called "getit" to easily trigger the request.
This sample code works fine as is.?I also added my own custom header "Mycustomheader": "foo" and I do see it included on the response from httbin.org. This tells me that the custom headers do work as intended.
?
However, the moment this line gets uncommented:
//request.Header.SetHeaderValue("Content-Length", payload.Length.ToString());
The whole thing breaks down. And I can't figure out why... any help would be greatly appreciated.
using System;
using Crestron.SimplSharp;? ? ? ? ? ? ? ? ? ? ? ? ? // For Basic SIMPL# Classes
using Crestron.SimplSharpPro;? ? ? ? ? ? ? ? ? ? ? ? // For Basic SIMPL#Pro classes
using Crestron.SimplSharpPro.CrestronThread;? ? ? ? // For Threading
using Crestron.SimplSharpPro.Diagnostics; ? ? // For System Monitor Access
using Crestron.SimplSharpPro.DeviceSupport;? ? ? ? ? // For Generic Device Support
using Crestron.SimplSharp.Net.Https;
?
namespace Simplified_Test
{
? ? public class ControlSystem : CrestronControlSystem
? ? {
? ? ? ? #region Private Fields
? ? ? ? private HttpsClient _httpsClient;
? ? ? ? #endregion
?
? ? ? ? public ControlSystem()
? ? ? ? ? ? : base()
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Thread.MaxNumberOfUserThreads = 20;
?
? ? ? ? ? ? ? ? //Subscribe to the controller events (System, Program, and Ethernet)
? ? ? ? ? ? ? ? CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(ControlSystem_ControllerProgramEventHandler);
?
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception e)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ErrorLog.Error("Error in the constructor: {0}", e.Message);
? ? ? ? ? ? }
? ? ? ? }
?
?
? ? ? ? public override void InitializeSystem()
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? CrestronConsole.AddNewConsoleCommand(ConsoleCommandGetIt, "getit", "sends a GET request to httpbin with a small payload", ConsoleAccessLevelEnum.AccessAdministrator); //for tests
?
? ? ? ? ? ? ? ? _httpsClient = new HttpsClient();
? ? ? ? ? ? ? ? _httpsClient.Accept = "application/json";
? ? ? ? ? ? ? ? _httpsClient.KeepAlive = true;
? ? ? ? ? ? ? ? _httpsClient.Timeout = 20;
?
? ? ? ? ? ? ? ? _httpsClient.HostVerification = false;? ?//set to false for now - to keep things simple
? ? ? ? ? ? ? ? _httpsClient.PeerVerification = false;? ?//set to false for now - to keep things simple
?
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception e)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ErrorLog.Error("Error in InitializeSystem: {0}", e.Message);
? ? ? ? ? ? }
? ? ? ? }
?
? ? ? ? #region Creates a request and dispatches it Async
? ? ? ? private void ConsoleCommandGetIt(string cmdParameters)
? ? ? ? {
? ? ? ? ? ? string url = "https://httpbin.org/anything";
? ? ? ? ? ? string payload = "{\"memberId\":123,\"resultId\":1,\"timestamp\":\"2022-02-09T18:18:04.000Z\"}";
? ? ?
?
? ? ? ? ? ? var request = new HttpsClientRequest();
? ? ? ? ? ? request.Url.Parse(url);
? ? ? ? ? ? request.RequestType = RequestType.Get;
? ? ? ? ? ? request.Header.ContentType = "application/json";
? ? ? ? ? ? request.Header.SetHeaderValue("myCustomHeader", "foo"); //adding a custom header just to see if it appears on the far end
? ? ? ? ? ??
?
? ? ? ? ? ? //adding the json payload to the body of this request
? ? ? ? ? ? request.ContentString = payload;
?
? ? ? ? ? ? //trying to add a Content-Length to the header
? ? ? ? ? ? //request.Header.SetHeaderValue("Content-Length", payload.Length.ToString());
? ? ? ? ? ? //CrestronConsole.PrintLine("The request header as built is: " + request.Header.ToString());
?
? ? ? ? ? ? CrestronConsole.PrintLine("The request payload is: " + request.ContentString);
? ? ? ? ? ??
? ? ? ? ? ? //trying to dispatch it async
? ? ? ? ? ? var result = _httpsClient.DispatchAsync(request, httpsClientResponseCallback);
? ? ? ? ? ? if (result > 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? CrestronConsole.PrintLine("There was an error with the dispatch async. Error code: " + (HttpsClient.DISPATCHASYNC_ERROR)result);
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? CrestronConsole.PrintLine("The request has been dispatched. Now waiting for a reply.");
? ? ? ? ? ? }
?
? ? ? ? }
? ? ? ? #endregion
?
? ? ? ? #region Here is the reply from the far end
? ? ? ? private void httpsClientResponseCallback(HttpsClientResponse response, HTTPS_CALLBACK_ERROR error)
? ? ? ? {
? ? ? ? ? ? CrestronConsole.PrintLine("The response for the request returned " + (HTTPS_CALLBACK_ERROR)error);
?
? ? ? ? ? ? if (error > 0) //if this is true, the response object will be null!!! Don't try to further process it.
? ? ? ? ? ? {
? ? ? ? ? ? ? ? CrestronConsole.PrintLine("httpsClientResponseCallback -> HTTPS_CALLBACK_ERROR=" + error);
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
?
? ? ? ? ? ? CrestronConsole.PrintLine("The response code was " + response.Code);? ? //In the realword, I should make some logic that will look for a 200 OK and properly manage error codes in a log somehow.
?
? ? ? ? ? ? CrestronConsole.PrintLine("The response Encoding was " + response.Encoding);
? ? ? ? ? ? CrestronConsole.PrintLine("The response ContentLength was " + response.ContentLength);
? ? ? ? ? ? CrestronConsole.PrintLine("The URL of whoever provided this response is " + response.ResponseUrl);
? ? ? ? ? ? CrestronConsole.PrintLine("The response.Header.FirstHeader is: " + response.Header.FirstHeader);? ? ? ?//this seems to always return empty.
? ? ? ? ? ? CrestronConsole.PrintLine("The response.Header.RequestVersion is: " + response.Header.RequestVersion); //this seems to always return empty.
?
? ? ? ? ? ? CrestronConsole.PrintLine("The response.Chunked is " + response.Chunked);
?
? ? ? ? ? ? CrestronConsole.PrintLine("The response entire Header was:\n " + response.Header.ToString());
?
? ? ? ? ? ? CrestronConsole.PrintLine("Here is the actual response body:");
? ? ? ? ? ? CrestronConsole.PrintLine(response.ContentString);
?
? ? ? ? }
? ? ? ? #endregion
?
? ? ? ? #region House Cleaning
? ? ? ? void ControlSystem_ControllerProgramEventHandler(eProgramStatusEventType programStatusEventType)
? ? ? ? {
? ? ? ? ? ? switch (programStatusEventType)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? case (eProgramStatusEventType.Stopping):
? ? ? ? ? ? ? ? ? ? _httpsClient.Dispose();
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
?
? ? ? ? }
? ? ? ? #endregion?
?
?
? ? ? ??
? ? }
}