gpt4 book ai didi

android - 发生了一个或多个错误(到 192.168.0.18 的纯文本 HTTP 流量不允许 Xamarin 表单

转载 作者:行者123 更新时间:2023-11-30 04:55:13 25 4
gpt4 key购买 nike

我需要帮助。我正在尝试使用“MultipartFormDataContent”和“HttpClient”上传文件,但出现错误。我看到有很多人处于同样的情况,但他们的解决方案对我不起作用。

event click to upload

private async void upload_Clicked(object sender, EventArgs e) {
var content = new MultipartFormDataContent();
content.Add(new StreamContent(_mediaFile.GetStream()),
"\"file\"",
$ "\"{_mediaFile.Path}\"");

var httpClient = new HttpClient();

var uploadServiceBaseAdress = "http://192.168.0.18/upload";

Console.WriteLine(_mediaFile.Path);
try {
var httpResponseMessage = httpClient.PostAsync(uploadServiceBaseAdress, content);
message.Text = httpResponseMessage.Result.Content.ToString();
} catch (Exception error) {
await DisplayAlert("non", error.Message, "ok");
}
}

manifest

<application android:networkSecurityConfig="@xml/network_security_config"></application>

Resources > xml > network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">192.168.0.18</domain>
</domain-config>
</network-security-config>

在线 var httpResponseMessage = httpClient.PostAsync(uploadServiceBaseAdress, content);我收到以下错误:

One or more errors occurred (Clea-text HTTP traffic to 192.168.0.18 not permitted Xamarin forms

有什么解决办法吗?

编辑它适用于使用 HTTPS 的生产服务器,但我需要它适用于开发服务器 (192.168.0.18)。我该如何解决这个问题

最佳答案

根据文件Network security configuration

实现这一点的简单方法是将此属性用于您的 AndroidManifest.xml,您允许所有请求的所有 http:android:usesCleartextTraffic="true"

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:usesCleartextTraffic="true"
...>
...
</application>

另一种方法:用base-config替换domain-config

XML res/xml/network_security_config.xml

 <?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>

并像这样在 AndroidManifest.xml 中使用:

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:networkSecurityConfig="@xml/network_security_config"
...>
...
</application>
</manifest>

关于android - 发生了一个或多个错误(到 192.168.0.18 的纯文本 HTTP 流量不允许 Xamarin 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59303279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com