У мегя есть такой скрипт:
public void Main()
{
string url = "https://api/test/test/test";
string sinceDateTimeUtc = "2020-01-01T00:00:00.000";
string tillDateTimeUtc = "2022-12-11T00:00:00.000";
string Authorization = "secretKey=\"fasfsdffsafdsafsdaveqwv\"";
var request = (HttpWebRequest)WebRequest.Create(url);
var postData = "{" + "sinceDateTimeUtc" + ":" + $"'{sinceDateTimeUtc}'" + "," + "tillDateTimeUtc" + ":" + $"'{tillDateTimeUtc}'" + "}"; //, tillDateTimeUtc : '2022-12-11 00:00:00.000' }";
var data = Encoding.UTF8.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = data.Length;
request.Accept = "application/json";
request.Headers.Add("Authorization", Authorization);
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
var jobject = JObject.Parse(responseString);
Console.WriteLine(jobject);
Dts.TaskResult = (int)ScriptResults.Success;
}
Когда я его запускаю, то получаю такую ошибку:
ошибка в ввиде текста:
в System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
в System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
в System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
в System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
в Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()
Я определлил что скрипт не работает из-за этого:
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
Потому что если запустить такой код: то скрипт отрабатывает без ошибки
public void Main()
{
string url = "https://api/test/test/test";
string sinceDateTimeUtc = "2020-01-01T00:00:00.000";
string tillDateTimeUtc = "2022-12-11T00:00:00.000";
string Authorization = "secretKey=\"fasfsdffsafdsafsdaveqwv\"";
var request = (HttpWebRequest)WebRequest.Create(url);
var postData = "{" + "sinceDateTimeUtc" + ":" + $"'{sinceDateTimeUtc}'" + "," + "tillDateTimeUtc" + ":" + $"'{tillDateTimeUtc}'" + "}"; //, tillDateTimeUtc : '2022-12-11 00:00:00.000' }";
var data = Encoding.UTF8.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = data.Length;
request.Accept = "application/json";
request.Headers.Add("Authorization", Authorization);
//using (var stream = request.GetRequestStream())
//{
// stream.Write(data, 0, data.Length);
//}
//var response = request.GetResponse();
//var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
//
//var jobject = JObject.Parse(responseString);
//Console.WriteLine(jobject);
Dts.TaskResult = (int)ScriptResults.Success;
}
Как испрвить это?