Xamarin Android 从 LAN 下载失败

Xamarin Android download from LAN failed

我有一个 xamarin android 应用程序,当我尝试从我在本地 IIS 托管的 MVC 站点下载图像时,它失败了。

防火墙关闭,在 genymotion 模拟器中我可以在 OS 浏览器上看到文件,但是当我通过我的 xamarin 应用程序下载文件时出现异常:NameResolutionFailureException.

android 已设置访问 Internet 的权限

using (var webClient = new WebClient())
{
    var imageByte = webClient.DownloadData("http://10.0.3.2/imgs/1.jpg");
}

您可能需要在您的 PC 上打开防火墙以接受来自您的模拟器的 IP 的远程连接(使用 adb shell ifconfig SushiHangover 建议查看您的模拟器的 IP 地址)

要设置防火墙规则以允许远程连接,请参阅本指南(它是关于 WCF 的,但无论使用何种服务技术,添加防火墙规则应该是相同的): https://developer.xamarin.com/guides/cross-platform/application_fundamentals/web_services/walkthrough_working_with_WCF/#Configuring_Remote_Access_to_IIS_Express

相关位(编辑以删除对 WCF 的引用):

Configure IIS Express to Accept Remote connections - This step involves editing the config file for IIS Express to accept remote connections on a specific port and then setting up a rule for IIS Express to accept the incoming traffic.

Add an Exception to Windows Firewall - We must open up a port through Windows Firewall that remote applications can use. You will need to know the IP address of your workstation. For the purposes of this example we'll assume that our workstation has the IP address 192.168.1.143.

Let's begin by configuring IIS Express to listen for external requests. We can do this by editing the configuration file for IIS Express at [solutiondirectory].vs\config\applicationhost.config, as shown in the following screenshot: Locate the site element with the name of your service. It should look something like the following XML snippet:

<site name="HelloWorldWcfHost" id="2">
    <application path="/" applicationPool="Clr4IntegratedAppPool">
        <virtualDirectory path="/" physicalPath="\vmware-host\Shared Folders\tom\work\xamarin\code\private-samples\webservices\HelloWorld\HelloWorldWcfHost" />
    </application>
    <bindings>
        <binding protocol="http" bindingInformation="*:9607:localhost" />
    </bindings>
</site>

We will need to add another binding to open up port 9608 to outside traffic. Add the following XML to the bindings element, replacing the IP address with your own IP address:

<binding protocol="http" bindingInformation="*:9608:192.168.1.143" />

This will configure IIS Express to accept HTTP traffic from any remote IP address on port 9608 on the external IP address of the computer. This above snippet assumes the IP address of the computer running IIS Express is 192.168.1.143. After the changes, the bindings element should look like the following:

<site name="HelloWorldWcfHost" id="2">
    <application path="/" applicationPool="Clr4IntegratedAppPool">
        <virtualDirectory path="/" physicalPath="\vmware-host\Shared Folders\tom\work\xamarin\code\private-samples\webservices\HelloWorld\HelloWorldWcfHost" />
    </application>
    <bindings>
        <binding protocol="http" bindingInformation="*:9607:localhost" />
        <binding protocol="http" bindingInformation="*:9608:192.168.1.143" />
    </bindings>
</site>

Next, we need to configure IIS Express accept incoming connections on port 9608. Startup up an administrative command prompt, and run this command:

netsh http add urlacl url=http://192.168.1.143:9608/ user=everyone

The final step is to configure Windows Firewall to permit external traffic on port 9608. From an administrative command prompt, run the following command:

netsh advfirewall firewall add rule name="IISExpressXamarin" dir=in protocol=tcp localport=9608 profile=private remoteip=localsubnet action=allow

This command will allow incoming traffic on port 9608 from all devices on the same subnet as the Windows 10 workstation.