[英]Windows Phone 8 call WCF web service
我的任务是编写一个需要链接到WCF Web服务的WP8应用程序,但我终生无法弄清楚自己在做什么错
我无法更改WCF服务,这不是我的
我正在努力的方法将一个类作为参数,并返回一个不同的类作为结果
我为测试方法提供的接口是:
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
UriTemplate = "TestLogin")]
TestResults TestLogin(TestDetails Test);
public class TestDetails
{
public string Username { get; set; }
public string Password { get; set; }
public string DeviceId { get; set; }
}
public string TestResult
{
public int TestId { get; set; }
public List<TestOrders> Orders { get; set; }
}
我已经尝试过RestSharp,但是我收到一个错误的请求
任何建议将不胜感激
这是我的示例代码:
var client = new RestClient
{
BaseUrl = "http://www.testing.co.uk/Services/Service.svc"
};
var dto = new TestDetails
{
username = "abc",
password = "123",
DeviceId = String.Empty,
DeviceModel = String.Empty
};
var request = new RestRequest
{
Resource = "Testlogin",
RequestFormat = DataFormat.Json,
Method = Method.POST
};
request.AddParameter("TestDetails", dto, ParameterType.RequestBody);
// request.AddBody(dto);
var response = client.Post<TestResult>(request);
OK弄清楚了:-)我已经切换到使用HttpClient
public static void Testing()
{
var c = new HttpClient
{
BaseAddress = new Uri("http://www.testing.co.uk/Services/Service.svc/")
};
c.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var json = "{\"login\":{\"DeviceId\":\"\",\"Password\":\"123\",\"Username\":\"abc\",\"DeviceModel\":\"\"}}";
var req = new HttpRequestMessage(HttpMethod.Post, "TestLogin")
{
Content = new StringContent(json, Encoding.UTF8, "application/json")
};
c.SendAsync(req).ContinueWith(respTask =>
{
var response = respTask.Result.Content.ReadAsStringAsync();
Console.WriteLine("Response: {0}", respTask.Result);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.