xamarin android:httpclient PostAsync
xamarin android : httpclient PostAsync
我们在 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);
}
}
感谢 link 您的提供。
我已经尝试了 postasync 上的缓冲区 / 尝试在 wifi 或 3G 中同步 / 删除 json 中的特殊字符 / ...
但没有任何效果
我们已将prod数据库移至test数据库,并尝试使用postman将数据同步到test数据库。邮递员的结果是实体太大!
Json 大小 > 1.2 兆,IIS 中的默认值设置为 1 兆
这就是问题所在...
感谢问题解决
我遇到了同样的问题。为这个问题奋斗了6个小时
如果您看到错误,我会收到(无法连接到 localhost/127.0.0.1:7113)。如果您将 localhost 放入您的浏览器或 swagger 工具中,它会起作用,但如果您将 https://127.0.0.1:7113/api/weatherforecast 放入您的浏览器中,它将不起作用。它会给你一个证书问题。
所以我认为您必须在本地开发机器上使用 https 证书将 127.0.0.1 解析为本地主机。
我正在使用 Visual Studio 2022 预览版构建 MAUI 应用程序。
所以我通过将 API 部署到 AZURE 解决了这个问题。
然后更新到 azure url 例如:
字符串 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.";
}
}
}
我们在 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);
}
}
感谢 link 您的提供。 我已经尝试了 postasync 上的缓冲区 / 尝试在 wifi 或 3G 中同步 / 删除 json 中的特殊字符 / ... 但没有任何效果
我们已将prod数据库移至test数据库,并尝试使用postman将数据同步到test数据库。邮递员的结果是实体太大!
Json 大小 > 1.2 兆,IIS 中的默认值设置为 1 兆
这就是问题所在...
感谢问题解决
我遇到了同样的问题。为这个问题奋斗了6个小时
如果您看到错误,我会收到(无法连接到 localhost/127.0.0.1:7113)。如果您将 localhost 放入您的浏览器或 swagger 工具中,它会起作用,但如果您将 https://127.0.0.1:7113/api/weatherforecast 放入您的浏览器中,它将不起作用。它会给你一个证书问题。
所以我认为您必须在本地开发机器上使用 https 证书将 127.0.0.1 解析为本地主机。
我正在使用 Visual Studio 2022 预览版构建 MAUI 应用程序。
所以我通过将 API 部署到 AZURE 解决了这个问题。
然后更新到 azure url 例如: 字符串 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.";
}
}
}