Using .Net in C# Invoke Code

Hello,

I’m trying to use an API call to upload a file to Azure for Video Indexing. The HTTP Request activity isn’t enough because I need to upload. I am using System.Net.Http methods, but am seeing the following errors like the following:

Invoke code: No compiled code to run
error CS1069: The type name ‘HttpClientHandler’ could not be found in the namespace ‘System.Net.Http’. This type has been forwarded to assembly ‘System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=####’ Consider adding a reference to that assembly. At line 9

My code is below. Any idea why the System.Net.Http is giving errors?

var apiUrl = "https://api.videoindexer.ai";
var accountId = "####"; 
var location = "trial";
var apiKey = "####"; 

System.Net.ServicePointManager.SecurityProtocol = 
System.Net.ServicePointManager.SecurityProtocol | System.Net.SecurityProtocolType.Tls12;

// create the http client
var handler = new System.Net.Http.HttpClientHandler(); 
handler.AllowAutoRedirect = false; 
var client = new System.Net.Http.HttpClient(handler);
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", apiKey); 

// obtain account access token
var accountAccessTokenRequestResult = client.GetAsync(apiUrl + "/auth/" + location + "/Accounts/" + accountId + "/AccessToken?allowEdit=true").Result;
var accountAccessToken = accountAccessTokenRequestResult.Content.ReadAsStringAsync().Result.Replace("\"", "");

client.DefaultRequestHeaders.Remove("Ocp-Apim-Subscription-Key");

// upload a video
var content = new System.Net.Http.MultipartFormDataContent();
FileStream video =File.OpenRead(@"C:\Users\admin\Downloads\myvideo.mp4");
byte[] buffer = new byte[video.Length];
video.Read(buffer, 0, buffer.Length);
content.Add(new System.Net.Http.ByteArrayContent(buffer));

var uploadRequestResult = client.PostAsync(apiUrl + "/" + location + "/Accounts/" + accountId + "/Videos?accessToken=" + accountAccessToken + "&name=some_name&description=some_description&privacy=private&partition=some_partition", content).Result;
jsonOutput = uploadRequestResult.Content.ReadAsStringAsync().Result;
1 Like

I think I figured it out. For others who may stumble upon this, you need to select “Manage Packages” in Studio. Then, under “nuget.org” search for “System.Net.Http”. Install that and Save.

2 Likes

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.