首页 > 解决方案 > 无法从 Android 调用 Web 服务,但它可以从 UWP 工作

问题描述

我必须在 Xamarin 项目中实现 Web 服务。所以我创建了一个简单的 Web 服务来做一些测试。有我以前的代码:

using (HttpClient client = new HttpClient()) {
                client.BaseAddress = new Uri("http://localhost:62056/");
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage response = client.GetAsync("DataTransferTest.svc/HelloWorld").Result;
                string json = response.Content.ReadAsStringAsync().Result;    
}

我已经尝试改变http://localhost:62056/tohttp:10.0.0.2:62056但没有任何改变。

我已经像这样配置了我的 ISS Express:

<site name="LogiStock.WebServices2" id="4">
                <application path="/" applicationPool="Clr4IntegratedAppPool">
                    <virtualDirectory path="/" physicalPath="D:\TeamExplorer\LogiStock-1.0.1\LogiStock.WebServices2" />
                </application>
                <bindings>
                    <binding protocol="http" bindingInformation="*:62056:localhost" />
                    <binding protocol="http" bindingInformation="*:62056:192.168.90.139" />
                    <binding protocol="http" bindingInformation="*:62056:127.0.0.1" />
                </bindings>
            </site>

当我尝试此代码时出现错误:

出现一个或多个错误。发送请求时出错_Error: ConnectFailure (Connection denied) _Connection denied

我真的希望有人能帮助我,我真的不知道出了什么问题..

标签: androidweb-serviceswcfxamarin

解决方案


只是为了确保您的清单文件中有以下行?

<uses-permission android:name="android.permission.INTERNET" /> 

推荐阅读