【问题标题】:How to access a WCF REST Service inside .Net web application?如何在 .Net Web 应用程序中访问 WCF REST 服务?
【发布时间】:2015-06-28 15:28:05
【问题描述】:

我有一个 WCF Web 服务,它从 Silverlight 应用程序返回一个 JSON 字符串。我想在另一个 Web 应用程序的控制器方法中解析这个 JSON 字符串。我无法在 Web 应用程序的 Silverlight 中创建对 WCF 服务的服务引用,因为它是一个 REST 服务。如何在其他应用程序中访问此 WCF REST 服务?

【问题讨论】:

  • @rene 因为它是一个 REST 服务我不想创建一个服务引用
  • 当然,我只是编辑了您的帖子以摆脱标签 MVC。我现在添加了 silverlight,因为它更符合您的问题

标签: asp.net wcf rest silverlight


【解决方案1】:

您应该使用 System.Net.WebRequest 之类的东西来调用控制器中的 WCF 服务。

网上有很多关于如何正确使用它的例子。

就个人而言,我在所有应用程序中都使用JSON.NetAngularJS

【讨论】:

    【解决方案2】:

    我可以使用以下代码访问网络服务

    using System.Net;
    
    public string GetWebServiceData()
        {
            try
            {
                string requestUrl = "requesturl";
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUrl);
                request.Method = WebRequestMethods.Http.Post;
                request.ContentType = "application/json";
                request.ContentLength = 0;
                request.Expect = "application/json";
                WebResponse response = request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream());
                string json = reader.ReadToEnd();
                return json;
            }
            catch (Exception)
            {
                return new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(string.Empty);
            }
    
       }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-17
      相关资源
      最近更新 更多