我读了很多关于使用HTTPClient与旧Web服务通信的文章。代码以xamarin的形式存在。NetStandard(System.Net.Http)我有一个简单的代码:
public async Task<bool> TestKelvinConnectivity()
{
bool retour = false;
using (var client = new HttpClient())
{
client.BaseAddress = _baseEnkiWeb;
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/soap+xml"));
string Enveloppe = "<soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope' xmlns:dun='http://Dunin.com'><soap:Header><dun:User></dun:User><dun:ClefSession>Login</dun:ClefSession><dun:Licence1>EnkiEve</dun:Licence1><dun:Licence2></dun:Licence2></soap:Header><soap:Body><dun:loadMaintenance></dun:loadMaintenance></soap:Body></soap:Envelope>";
var content = new StringContent(Enveloppe, Encoding.UTF8, "application/soap+xml");
try
{
var response = await client.PostAsync(_baseEnkiWeb.ToString(), content);
var soapResponse = await response.Content.ReadAsStringAsync();
if (soapResponse.Contains("ErreurAuthentification") == true)
{
throw new Exception("ErreurLoadMaintenance");
}
XDocument soap = XDocument.Parse(soapResponse);
XNamespace ns = "http://Dunin.com";
}
catch (Exception ex)
{
throw ex;
}
}
return false;
}
在UWP上,代码运行良好,我在回复后收到了答案。所容纳之物ReadAsStringAsync();
但在Android上,我可以跟踪代码,直到等待客户端。PostAsync(…)然后调试器跳转函数末尾的“return false”。没有例外,没有错误,Android设备日志中唯一的内容是:NetworkSecurityConfig:没有指定网络安全配置,使用平台默认设置。
知道吗?该URL位于开发计算机的本地,可从android模拟器设备浏览器访问。
编辑1:
当我删除PostAsync方法前面的wait并添加。后果但我不明白为什么。这段代码适用于UWP!这是我的修改(soapResponse获得了正确的数据):
var response = client.PostAsync(_baseEnkiWeb.ToString(), content).Result;
var soapResponse = await response.Content.ReadAsStringAsync();