HttpClient后记录,状态OK但记录未创建且空响应

时间:2018-02-25 14:41:08

标签: c# asp.net web-services httpclient integration

我有一个使用HttpClient的插件,它应该在外部ERP数据库中创建一个记录。我发送请求,我身边的一切都没问题,我收到状态OK但是response.Content.ReadAsStringAsync()为空。在ERP数据库中没有创建记录。代码

      public class OnPostCreate : BaseClass
{
    private static Uri baseAdress = new Uri("http://adress/ws/Methodname.json?raw=");
    private static HttpClient client = new HttpClient();
    public static string trace = "";
    public override void ExecutePlugin()
    {
        Account account = ((Entity)context.InputParameters["Target"]).ToEntity<Account>();
        Account getAcc = dataContext.AccountSet.Where(p => p.Id == account.Id)
            .Select(p => new Account()
            {
                Id = p.Id,
                jar_id = p.jar_id,
                jar_field1= p.jar_field1,
                jar_field2= p.jar_field2,
                jar_field3= p.jar_field3,
                jar_field4= p.jar_field4
            })
            .SingleOrDefault();      

        if (getAcc == null) return;

        ModelClass model = CreateModel(getAcc);
        string content = GetContent(model);

        Account acc = new Account
        {
            Description = baseAdress + content,
            Id = getAcc.Id
        };
        service.Update(acc);

        RunAsync(content, tracingService).GetAwaiter().GetResult();
        tracingService.Trace(trace);
    }

    private static async Task RunAsync(string content, ITracingService tracingService)
    {
        client.BaseAddress = baseAdress;
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
        try
        {
            await SendRequest(HttpMethod.Post, content);
        }
        catch (Exception e)
        {
            throw new InvalidPluginExecutionException(e.Message + e.InnerException + e.InnerException.InnerException);
        }
    }

    private static async Task SendRequest(HttpMethod method, string query)
    {
        HttpRequestMessage request = new HttpRequestMessage(method, query);
        HttpResponseMessage response = await client.SendAsync(request);
        string responsetext = await response.Content.ReadAsStringAsync();
        trace += "Code:" + response.StatusCode.ToString();
        trace += "/n Response: " + responsetext;
        response.EnsureSuccessStatusCode();
    }

    private ModelClass CreateModel(Account account) => new ModelClass()
    {
        Id = account.jar_id,
        field4= account.jar_field4,
       field1 = Convert.ToInt32(account.jar_field1 ),
        field2= Convert.ToInt32(account.jar_field2),
        field3= account.jar_field3
    };

    private string GetContent(ClassModel model) => $"<RequestData>" +
                        $"<Id>{model.Id}</Id>" +
                        $"<field1>{model.field1}</field1>" +
                        $"<field2>{model.field2}</field2>" +
                        $"<field3>{model.field3}</field3>" +
                        $"<field4>{model.field4}</field4>" +
                        $"</RequestData>";
}

请帮忙!任何建议为什么在我身边一切似乎都没问题(但响应是空的??)而另一方面甚至没有请求标记。

我将请求网址粘贴到邮递员,并且在第一次尝试时我没有得到任何响应,第二个:503错误对象引用未设置为DataService的DataService.Modules.Json.JsonHandler.Process(HttpListenerContext context)中的对象实例.Common.HttpServer.BeginGetContextCompleted(IAsyncResult result)

此致

0 个答案:

没有答案