【问题标题】:xamarin android : httpclient PostAsyncxamarin android:httpclient PostAsync
【发布时间】:2020-06-09 14:14:27
【问题描述】:

我们在 xamarin android 下使用 Visual Studio 2017 构建了一个应用程序。 这个应用程序运行三年没有任何问题。

两周以来,我不知道为什么有些设备实际上无法与我们的后端同步。 这真的很奇怪,因为这部分没有任何变化。

此错误并非出现在所有设备上,而是不时出现在一两个设备上 我们使用 dll httpClient 与我们的后端同步数据。

如果我在 postAsync 中放置一个断点,我有一个例外 -> 无法访问已处置的对象。对象名称:'System.Net.Sockets.NetworkStream

有人知道如何解决这个问题吗?还有什么意思?

这是 postAsync 方法的代码: 感谢我们的时间和评论家伙

public override HttpResult ExecutePost(Uri target, string body)
    {
        var client = new HttpClient();
        client.MaxResponseContentBufferSize = MaxHttpResponseBufferSize;

        try
        {
           var requestContent = new StringContent(body, RequestContentEncoding, RequestContentType);
           var response = client.PostAsync(target, requestContent).Result;
           if (response.IsSuccessStatusCode)
           {
              var content = response.Content.ReadAsStringAsync().Result;
              return new HttpResult(content, null, null);
           }

           return new HttpResult(null, "Response is empty", response.StatusCode.ToString());
        }
        catch (Exception e)
        {
            return new HttpResult(null, "Problem with the HttpPost", e.Message);
        }
    }

【问题讨论】:

标签: android xamarin httpclient


【解决方案1】:

我遇到了同样的问题。在这个问题上已经奋斗了 6 个小时。

如果您阅读错误,我得到了(无法连接到 localhost/127.0.0.1:7113)。如果您将 localhost 放入浏览器或 swagger 工具中,它将起作用,但如果您将 https://127.0.0.1:7113/api/weatherforecast 放入浏览器中,它将不起作用。它会给你一个证书问题。

所以我认为您必须在本地开发机器上使用 https 证书将 127.0.0.1 解析为 localhost。

我正在使用 Visual Studio 2022 Preview 构建一个 MAUI 应用。

所以我通过将我的 API 部署到 AZURE 解决了这个问题。

然后更新到 azure url 例如: string apiUrl = "https://weatherforecast.azurewebsites.net/api/weatherforecast";

然后它工作得很好。喜欢超级精彩。

这是我的代码:

 public void LoginAsync()
 {
        HttpClient client = new HttpClient();

        string apiUrl = "https://weatherforecast.azurewebsites.net/api/weatherforecast";

        UserCredentials.EmailAddress = LoginUIEntity.EmailAddress;
        UserCredentials.Password = LoginUIEntity.Password;

        string serialized = JsonConvert.SerializeObject(UserCredentials);

        var inputMessage = new HttpRequestMessage
        {
            Content = new StringContent(serialized, Encoding.UTF8, "application/json")
        };

        inputMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        try
        {
            var message = client.PostAsync(apiUrl, inputMessage.Content).Result;

            if (message.IsSuccessStatusCode)
            {
                var apiResponse = message.Content.ReadAsStringAsync();
                UserCredentials = JsonConvert.DeserializeObject<UserCredentials>(apiResponse.Result);

                if (UserCredentials.IsValid)
                {
                    UserCredentials.IsLoggedIn = true;
                }
                else
                {
                    ErrorMessage = "Invalid credentials supplied.";
                }
            }
        }
        catch (Exception ex)
        {
           ErrorMessage = "An error has occurred. Please contact support if the error persists.";
        }
    }
}

【讨论】:

    【解决方案2】:

    感谢您提供的链接。 我已经尝试了 postasync 上的缓冲区 / 尝试在 wifi 或 3G 中同步 / 删除 json 中的特殊字符 / ... 但没有任何效果

    我们已将 prod 数据库移至 test 并尝试使用 postman 将数据同步到 test 数据库。使用邮递员的结果是实体太大!

    Json 大小 > 1.2 兆,IIS 内部的默认值设置为 1 兆

    这就是问题所在......

    感谢问题解决

    【讨论】:

    • 感谢分享。不要忘记接受答案哦~
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多