Upgrading .NET version of GH

Hello,

I am trying to create HTTP Post and Get components using C# in Grasshopper. Because I need to set the Content-Type to json to communicate with my MIR-100, I cannot use WebClient. My solution is to use the more recent HttpClient, but I cannot import it using System.Net.Http. I suspect it is because my .Net version in GH (RH7) is 4.0. How can I use a later version?

Thanks,

V

The last GH uses .Net 4.8

That is .NET 4.8

Since you are using a C# scripting component, you will need to add a reference to the assembly that contains HttpClient which is System.Net.Http.dll

Thank, now I can use HttpClient.

However, I am still getting an Error 400 from my MIR-100.

This is my code:

using System.Net.Http;
using Newtonsoft.Json;

/// <summary>
/// This class will be instantiated on demand by the Script component.
/// </summary>
public class Script_Instance : GH_ScriptInstance
{
#region Utility functions
  /// <summary>Print a String to the [Out] Parameter of the Script component.</summary>
  /// <param name="text">String to print.</param>
  private void Print(string text) { /* Implementation hidden. */ }
  /// <summary>Print a formatted String to the [Out] Parameter of the Script component.</summary>
  /// <param name="format">String format.</param>
  /// <param name="args">Formatting parameters.</param>
  private void Print(string format, params object[] args) { /* Implementation hidden. */ }
  /// <summary>Print useful information about an object instance to the [Out] Parameter of the Script component. </summary>
  /// <param name="obj">Object instance to parse.</param>
  private void Reflect(object obj) { /* Implementation hidden. */ }
  /// <summary>Print the signatures of all the overloads of a specific method to the [Out] Parameter of the Script component. </summary>
  /// <param name="obj">Object instance to parse.</param>
  private void Reflect(object obj, string method_name) { /* Implementation hidden. */ }
#endregion

#region Members
  /// <summary>Gets the current Rhino document.</summary>
  private readonly RhinoDoc RhinoDocument;
  /// <summary>Gets the Grasshopper document that owns this script.</summary>
  private readonly GH_Document GrasshopperDocument;
  /// <summary>Gets the Grasshopper script component that owns this script.</summary>
  private readonly IGH_Component Component;
  /// <summary>
  /// Gets the current iteration count. The first call to RunScript() is associated with Iteration==0.
  /// Any subsequent call within the same solution will increment the Iteration count.
  /// </summary>
  private readonly int Iteration;
#endregion

  /// <summary>
  /// This procedure contains the user code. Input parameters are provided as regular arguments,
  /// Output parameters as ref arguments. You don't have to assign output parameters,
  /// they will have a default value.
  /// </summary>
  private void RunScript(string url, string JSONcontent, string authorization, int timeout, bool upload, ref object response, ref object authorizationOutput, ref object postOutput)
  {
    if (url == null) return;
    if (upload == false) return;





    postOutput = POSTData(JSONcontent, url);
  }

  // <Custom additional code> 
  public bool POSTData(object json, string url)
  {
    using (var content = new StringContent(JsonConvert.SerializeObject(json), System.Text.Encoding.UTF8, "application/json"))
    {
      HttpClient _httpClient = new HttpClient();
      _httpClient.DefaultRequestHeaders.Add("Authorization", "Basic QW5kcmVhOjVmM2Q2OTUyYzVjNWUyMjA3N2ZhYmY0NjFkZTgwZjFjZTQ3NTc1MmZlNzVhZmNmNWNhNDZiYWM0Mzg0MDU2MTk=");


      HttpResponseMessage result = _httpClient.PostAsync(url, content).Result;
      if (result.StatusCode == System.Net.HttpStatusCode.Created)
        return true;

      string returnValue = result.Content.ReadAsStringAsync().Result;

      throw new Exception("Failed to POST data: ({" + result.StatusCode + " }): {" + returnValue + "}");

    }
  }