2

我想通过 C# 向以下端点发出获取请求。

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

我正在查看 WCF rest starter kit,想知道除了安装 rest starter kit 之外,还有其他方法可以轻松地发出获取请求。

请建议。谢谢,

4

2 回答 2

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;
        }  
于 2012-06-18T13:54:49.047 回答
1

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

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

希望有帮助!

于 2012-06-18T13:49:13.890 回答