我正在尝试从Windows Phone 8.1应用程序调用WCF服务,添加服务引用的选项不再存在。
我试过了:
HttpClient httpClient = new System.Net.Http.HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://serverURl/serviceName.svc/methodName?variableName=value");
HttpResponseMessage response = await httpClient.SendAsync(request);
string data = await response.Content.ReadAsStringAsync();
但它没有用,字符串总是空的。
N.B 服务器也不是本地主机,因此我不会面临从Windows手机模拟器连接到本地主机的问题。
答案 0 :(得分:2)
试试这个。 如果它不起作用,请告诉我。
HttpClient httpClient = new System.Net.Http.HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get,"http://localhost:18362/Service1.svc/GetData");
HttpResponseMessage response = await httpClient.SendAsync(request);
string data = await response.Content.ReadAsStringAsync();
var dialog = new MessageDialog(data);
await dialog.ShowAsync();
答案 1 :(得分:1)
答案 2 :(得分:0)
您可以使用System.Net.Http:
public async Task<string> GetMethod(string url)
{
HttpClient client = new HttpClient();
var response = await client.GetAsync(url); // The Get Process to get result from WCF service
string result = await response.Content.ReadAsStringAsync(); // Get Json string result
return result;
}
}