如何通过c#使用Web服务端点

时间:2012-06-18 13:44:30

标签: c# asp.net .net rest wcf-rest

我想通过C#向以下终点发出get请求。

http://api.example.com/1/companies/ {ID}

我正在查看WCF休息入门套件,并且想知道还有其他方法可以轻松地获取请求,而不是安装其他入门套件。

请建议。谢谢,

2 个答案:

答案 0 :(得分:3)

这是一个从URL执行GET并返回字符串的方法:

public static string GetResponse(string endPoint)
        {
            HttpWebRequest request = CreateWebRequest(endPoint);

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                var responseValue = string.Empty;

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    string message = String.Format("POST failed. Received HTTP {0}", response.StatusCode);
                    throw new ApplicationException(message);
                }

                // grab the response  
                using (var responseStream = response.GetResponseStream())
                {
                    using (var reader = new StreamReader(responseStream))
                    {
                        responseValue = reader.ReadToEnd();
                    }
                }

                return responseValue;
            }
        }

编辑:

private static HttpWebRequest CreateWebRequest(string endPoint)
        {
            var request = (HttpWebRequest)WebRequest.Create(endPoint);

            request.Method = "GET";
            request.ContentLength = 0;
            request.ContentType = "text/json";

            return request;
        }  

答案 1 :(得分:1)

创建一个新的类库项目并将其命名为“RESTService.Lib”。添加对“System.ServiceModel”和“System.ServiceModel.Web”的引用。创建一个名为IRESTDemoServices的Interface类,并添加表示所提供服务的方法的定义。我们的界面将提供如下服务:

public interface IRESTDemoServices
{
    string GetClientNameById(string Id);
}

为了告诉框架将此接口视为服务,您需要按如下方式对其进行修饰:

[ServiceContract(Name = "RESTDemoServices")]
public interface IRESTDemoServices
{
    [OperationContract]
    string GetClientNameById(int Id);
}

定义用于访问服务的URL(URL路由):

public static class Routing
{
    public const string GetClientRoute = "/Client/{id}";
}

通过使用如下属性修饰接口来实现URL Route与接口中方法的连接:

[ServiceContract(Name = "RESTDemoServices")]
public interface IRESTDemoServices
{
    [OperationContract]
    [WebGet(UriTemplate = Routing.GetClientRoute, BodyStyle = WebMessageBodyStyle.Bare)]
    string GetClientNameById(string Id);
}

现在实施服务:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, 

                 ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class RestDemoServices:IRESTDemoServices
{
    public string GetClientNameById(string Id)
    {
        Random r = new Random();
        string ReturnString="";
        int Idnum=Convert.ToInt32(id);
        for (int i = 0; i < Idnum; i++)
            ReturnString += char.ConvertFromUtf32(r.Next(65, 85));

        return ReturnString;

    }
}

然后你必须在IIS中或通过使用自定义主机(控制台,Windows服务等)来托管它。

请看这里:http://www.progware.org/blog/post/a-simple-rest-service-in-c.aspx

希望有所帮助!