[英]Windows Phone 8 call WCF web service
I have been tasked with writing a WP8 application that needs to link to a WCF web service but I can't for the life of me figure out what I am doing wrong 我的任务是编写一个需要链接到WCF Web服务的WP8应用程序,但我终生无法弄清楚自己在做什么错
I can't change the WCF service, it's not mine 我无法更改WCF服务,这不是我的
The method I am struggling with takes a class as a parameter and returns a different class as the result 我正在努力的方法将一个类作为参数,并返回一个不同的类作为结果
The interface I have been given for a test method is: 我为测试方法提供的接口是:
[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; }
}
I have tried RestSharp but I just get a bad request 我已经尝试过RestSharp,但是我收到一个错误的请求
Any advice will be much appreciated 任何建议将不胜感激
Here's my sample code: 这是我的示例代码:
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 figured it out :-) I've switched to using HttpClient 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.