SSIS - PostAsJsonAsync 不工作

SSIS - PostAsJsonAsync is not working

我正在尝试在 SSIS 中使用一个 WEB API package.I 想在一个服务器文件夹中 post 一个 pdf 文件。我为此创建了一个脚本任务。

但是当我执行那个包时,它给了我一些随机的运行时错误。

byte[] content = System.IO.File.ReadAllBytes("Testfile.pdf");
string serviceUrl = "http://NNN.NN.NNN.NN:NNNN/";
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(serviceUrl);
// Add an Accept header for JSON format.  
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));

decimal amount = 1200.50m;
long tranactionID = 1001;

string APIUrl = "api/UploadDocCon/PDA/Test.pdf/FlexField/ABC/ABCD?FolderName=FoldA";
HttpResponseMessage response = client.PostAsJsonAsync(APIUrl, content).Result;


if (response.IsSuccessStatusCode)
{
  var serviceResponse = response.Content.ReadAsAsync<DocumentServiceResponse>().Result as DocServiceResponse;
return serviceResponse.STREAMID;
}
else
{
string error = response.Content.ReadAsStringAsync().Result;
}

它给我这个错误(它甚至没有命中调试器)

Exception has been thrown by the target of an invocation

 at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] 
arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, 
Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags 
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, 
Binder binder, Object target, Object[] providedArgs, ParameterModifier[] 
modifiers, CultureInfo culture, String[] namedParams)
at  Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()

当我替换这一行时

HttpResponseMessage response = client.PostAsJsonAsync(APIUrl, content).Result;

与 var response = client.GetAsync(APIUrl).Result;

它没有给出任何错误。但我想 post 一些字节数组在这里。

我做错了什么?

您可以使用 HttpClient 传输字节数组,方法是将 byte[] 转换为 System.Net.Http.HttpContent

代码片段如下:

ByteArrayContent byteContent = new ByteArrayContent(content); HttpResponseMessage reponse = await client.PostAsJsonAsync(uri, byteContent);