System.Net.WebException: 无法连接到远程服务器 - 仅限 LAN 中的服务器

System.Net.WebException: Unable to connect to Remote Server - Server in LAN only

为了为我的公司开发一个 Windows 通用应用程序,我需要连接到我们的本地网络服务器。为了测试这一点,我在我的开发机器上安装了一个 xampp,我用它为一些 Windows CE 设备开发了大约 5 个应用程序,到目前为止没有任何问题。现在我们也想使用平板电脑,用于非常具体的用例。

我的第一个 ModernUI 应用程序的唯一目的是向服务器发送 WebRequest 并使用我正在使用的代码在 TextBox 中显示响应:

    public async Task<string> Request(string uri)
    {
        WebRequest request = WebRequest.Create(uri);
        request.Credentials = CredentialCache.DefaultCredentials;
        HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
        Stream dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);
        string responseFromServer = reader.ReadToEnd();
        return responseFromServer;
    }

如果我 运行 这个应用程序在我的本地机器上(网络服务器是本地主机),一切正常。但是如果我 运行 它在平板电脑上,我会得到以下异常:

System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: Der Zugriff auf einen Socket war aufgrund der Zugriffsrechte des Sockets unzulässig 10.0.0.146:80 (Translation: Access to a socket was forbidden by its access permissions for the socket)

at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)\r\n at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)\r\n --- End of inner exception stack trace ---\r\n at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)\r\n at System.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func2 endFunction, Action1 endAction, Task1 promise, Boolean requiresSynchronization)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()\r\n at WaWiDemoWin8.ServerRequest.<Request>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()\r\n at WaWiDemoWin8.MainPage.d__0.MoveNext()"

请注意,可以通过浏览器访问网络服务器,如果我下载像 google.com 这样的在线网页,它就可以正常工作(从平板电脑)。我什至在另一台机器上安装了一个新的网络服务器来测试它,但是当我尝试在我们的 LAN 中访问这个网络服务器时,只会得到相同的异常。

我希望,至少有人可以给出解决方案或提示,因为我真的不知道问题是什么引起的。如果您需要更多信息,请告诉我,我会尽快提供。

谢谢, 丹尼尔

我假设这是一个商店应用程序。请注意,您的应用似乎没有访问网络的权限。

如果您阅读 App capability declarations (Windows Runtime apps),您将看到:

While you’re developing and testing your Windows Phone Store app app in Visual Studio, you get the networking capability (Internet (Client & Server)) when you run the app, even if you haven’t specified this capability in the app manifest file. When you publish your app, however, your app does not automatically get the networking capability. Make sure you check the Internet (Client & Server) capability on the Capabilities page of Manifest Designer if your app requires network connectivity.

您需要在应用程序清单中向应用程序功能添加网络权限之一。