小编mti*_*923的帖子

使用C#HttpClient api和postman测试之间的区别?客户端调用适用于邮递员但不适用于C#httpClient getAsync

在此输入图像描述在此输入图像描述我正在测试一个休息API帖子,当我在Postman上试用它时效果很好.但是,在某些情况下(与发布XML数据相关)如果我使用HttpClient API发布,我会收到以下错误:

"无法从传输连接读取数据:远程主机强行关闭现有连接."

但是相同的xml内容在Postman上工作正常,状态正常且响应正确.

任何人都知道使用C#HttpClient api和postman测试之间的区别是什么?如何配置我的api调用以匹配邮递员的行为?

这里我附上了源代码和Postman截图

public void createLoan()
{
    string baseCreateLoanUrl = @"https://serverhost/create?key=";
    var strUCDExport = XDocument.Load(@"C:\CreateLoan_testcase.xml");

    using (var client = new HttpClient())
    {
        var content = new StringContent(strUCDExport.ToString(), Encoding.UTF8, Mediatype);
        string createLoanApi = string.Concat(baseCreateLoanUrl, APIKey);

        try
        {
            var response = client.PostAsync(createLoanApi, content).Result;
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error Happened here...");
            throw;
        }

        if (response.IsSuccessStatusCode)
        {
            // Access variables from the returned JSON object
            string responseString = response.Content.ReadAsStringAsync().Result;
            JObject jObj = JObject.Parse(responseString);

            if (jObj.SelectToken("failure") == null)
            {
                // …
Run Code Online (Sandbox Code Playgroud)

c# api rest httpclient postman

6
推荐指数
2
解决办法
4459
查看次数

标签 统计

api ×1

c# ×1

httpclient ×1

postman ×1

rest ×1