我在Visual Studio 2013中开发了MVC 4项目,我也有第三方服务的数据,比如
http://245.245.245.245/testapi/Service1.svc?wsdl
我如何在我的MVC控制器中集成第三方服务并在Razor Views(.cshtml)上显示它。
提供建议代码或任何示例......
答案 0 :(得分:0)
您可以通过在Web项目中添加服务引用来使用服务。它的方法将可用,您将能够在您的Web项目中调用这些方法。
如果出于某些安全原因,您无法直接使用此功能,则可以使用HttpWebRequest:
var address = new Uri("https://yourServiceAddress");
var request = WebRequest.Create(address) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/json";
//Your parameters that you need to pass
var requestObject = new RequestJson()
{
userName = username,
password = password
};
var requestJson = JsonConvert.SerializeObject(requestObject);
var byteData = Encoding.UTF8.GetBytes(requestJson);
request.ContentLength = byteData.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(byteData, 0, byteData.Length);
}
using (var response = request.GetResponse() as HttpWebResponse)
{
var reader = new StreamReader(response.GetResponseStream());
Console.WriteLine(reader.ReadToEnd());
}
答案 1 :(得分:0)