【问题标题】:Problems with II7 and IIS ExpressII7 和 IIS Express 的问题
【发布时间】:2017-04-28 06:09:06
【问题描述】:

我有一个向 ASP.NET WEBAPI 服务发出 GET 请求的 android 应用程序 我已经创建了该服务,当您在使用 II Express 时在浏览器中打开它时它可以工作。但是,我的 android device() 中的应用程序无法访问我使用 URL:http:/localhost:8306/api/uconnectservice 创建的服务 收到的LOGCAT如下: java.net.SocketTimeoutException:10000 毫秒后无法连接到 /localhost(端口 8306)。我已激活 IIS7 并尝试查看 NET。我不知道这是否是服务器配置问题。请提供任何帮助或指导 谢谢 我的安卓代码: 字符串 URL = "http://localhost:8306/api/uconnectservice";

    private final OkHttpClient client = new OkHttpClient();

    public void run() throws Exception {
        Request request = new Request.Builder().url(URL).build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();

            }

            @SuppressLint("NewApi")
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try (ResponseBody responseBody = response.body()) {
                    if (!response.isSuccessful())
                        throw new IOException("Unexpected code " + response);

                    Headers responseHeaders = response.headers();
                    for (int i = 0, size = responseHeaders.size(); i < size; i++) {
                        System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
                    }

                    Log.e("Results", responseBody.string());
                }
            }
        });
    }

/////////////////////CODE FOR SERVER ASP.NET WEBAPI////////////////////
namespace UnicsPlcServices.Controllers
{
    public class UConnectServiceController : ApiController
    {
        private IUconnectRepository _UconnectHandler;

        public UConnectServiceController()
        {
            _UconnectHandler = new UconnectRepository();
        }

        public UConnectServiceController(IUconnectRepository repository)
        {
            if (_UconnectHandler == null)
            {
                throw new ArgumentNullException("repository");
            }
            _UconnectHandler = repository;
        }

        // GET api/uconnectservice
        public List<AcountTypesRegistered> Get()
        {
            return _UconnectHandler.GetAll();
        }
.........................other code`enter code here`

【问题讨论】:

  • 所以你尝试连接到localhost:8306,对吗?您确定服务器在同一台设备(Android 设备上)上运行吗?
  • @Vlad WebApi 应用程序正在 IIS Express 上运行,我只是从那里获取地址 localhost:8306 并在 android 应用程序中输入请求 URL。这是错的吗,我可以改变什么来让它工作
  • 您需要将localhost替换为您的服务器正在运行的设备的IP地址。
  • @Vlad 我做了你所说的但在 LOGCAT 中仍然有相同的消息:java.net.SocketTimeoutException:10000 毫秒后无法连接到 /10.0.2.2(端口 8306)。我正在使用 Visual Studio 附带的默认 IIS Express
  • 您的设备是否与服务器连接到同一个网络?你确定你使用正确的IP吗?

标签: android asp.net-web-api


【解决方案1】:

我有类似的问题。我使用了带有 IIS express 的 android studio 和 visual studio。我的解决方案是使用经典的 IIS,在 Android Studio 中我使用 10.0.2.2 而不是 localhost

IIS Express:你可以使用它,但你必须首先允许外部请求。 (http://www.ryadel.com/en/iis-express-allow-external-requests-remote-clients-devices/)

【讨论】:

  • 我正在使用 Eclipse+visual studio+IIS express。我只是在 android 的 URL 中使用了给我的服务的地址,即localhost:8306(见问题)。这是不是错了。我已经在我的机器上激活了 IIS7,但不知道如何托管 pls
  • 我搞错了。 IP 应为 10.0.2.2:8306。先试试这个。如果您不需要调试 Visual Studio 服务,只需将 Release(或可能是 Debug)文件夹的内容从项目目录复制到新的 IIS 站点目录。 (创建站点很简单 - 只需选择目录、站点名称和端口)
  • 我在 LOGCAT 中得到的错误信息是:java.net.SocketTimeoutException: failed to connect to /10.0.2.2 (port 8306) after 10000ms.....
  • 您确定 Web 服务在 IIS 上可用吗? (确保 IIS Express 没有运行)
  • IIS Express 已拒绝远程连接(安卓模拟器)。如果你想使用它,你必须启用它。 (例如stackoverflow.com/questions/3313616/…
【解决方案2】:

您的第一个问题是您没有使用运行 Web API 的机器的 IP 地址,您需要在 Java 代码中使用它而不是 localhost。

您的第二个问题是 IIS Express 不允许从本地主机访问,但为了解决这个问题,我们制作了一个名为 Conveyor 的免费 Visual Studio 扩展,您可以从 VS 中的扩展对话框或此处 https://visualstudiogallery.msdn.microsoft.com/a429dbb7-a982-4541-b401-934375c02c0f?redir=0

无需接触配置文件,它就会打开 IIS Express,因此您可以从其他机器(例如平板电脑和手机)连接到它。

【讨论】:

  • @user3015410 我更新了答案以包含您已经从其他 cmets 知道的信息。
猜你喜欢
  • 1970-01-01
  • 2015-05-09
  • 1970-01-01
  • 2013-11-05
  • 2023-03-26
  • 1970-01-01
  • 2010-11-20
  • 2018-09-10
  • 1970-01-01
相关资源
最近更新 更多