发生了一个或多个错误(到 192.168.0.18 的纯文本 HTTP 流量不允许 Xamarin 表单
One or more errors occurred (Clea-text HTTP traffic to 192.168.0.18 not permitted Xamarin forms
我需要帮助。我尝试使用 "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>
我需要帮助。我尝试使用 "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>