gpt4 book ai didi

c# - SSIS - PostAsJsonAsync 不工作

转载 作者:行者123 更新时间:2023-11-30 23:04:35 28 4
gpt4 key购买 nike

我正在尝试使用 SSIS 包中的一个 WEB API。我想在一个服务器文件夹中发布一个 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;

它不会给出任何错误。但我想在这里发布一些字节数组。

我做错了什么?

最佳答案

您可以使用 HttpClientbyte[] 转换为 System.Net.Http.HttpContent 来传输字节数组。

这是代码片段:

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

关于c# - SSIS - PostAsJsonAsync 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49356504/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com