C#代码中的RESTful web服务

C#代码中的RESTful web服务,c#,service,rest,C#,Service,Rest,在没有wcf的情况下,如何使用C代码中的RESTful web服务?一些非常简单的东西使用WebRequest类。请参阅。使用WebRequest类。请参阅。查看该项目-它是一个面向Asp.net的REST体系结构解决方案。查看该项目-它是一个面向Asp.net的REST体系结构解决方案。请使用以下代码调用RESTful web服务 string responseMessage; HttpClient client = new HttpClient(serviceUrl); HttpWebReq

在没有wcf的情况下,如何使用C代码中的RESTful web服务?一些非常简单的东西

使用WebRequest类。请参阅。

使用WebRequest类。请参阅。

查看该项目-它是一个面向Asp.net的REST体系结构解决方案。

查看该项目-它是一个面向Asp.net的REST体系结构解决方案。

请使用以下代码调用RESTful web服务

string responseMessage;
HttpClient client = new HttpClient(serviceUrl);
HttpWebRequest request = WebRequest.Create(string.Concat(serviceUrl, resourceUrl)) as HttpWebRequest;
request.ContentType = "text/xml";
request.Method = method;
HttpContent objContent = HttpContentExtensions.CreateDataContract(requestBody);
if(method == "POST" && requestBody != null)
{
    //byte[] requestBodyBytes = ToByteArrayUsingXmlSer(requestBody, "http://schemas.datacontract.org/2004/07/XMLService");
    byte[] requestBodyBytes = ToByteArrayUsingDataContractSer(requestBody);
    request.ContentLength = requestBodyBytes.Length;
    using (Stream postStream = request.GetRequestStream())
        postStream.Write(requestBodyBytes, 0, requestBodyBytes.Length);
    //request.Timeout = 60000;
}

HttpWebResponse response = request.GetResponse() as HttpWebResponse;
if(response.StatusCode == HttpStatusCode.OK)
{
    Stream responseStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(responseStream);

    responseMessage = reader.ReadToEnd();
}
else
{
    responseMessage = response.StatusDescription;
}
上述代码需要引用以下名称空间:

  • 使用Microsoft.Http;-->可从REST初学者工具包(Microsoft.Http.dll)获得

  • Net系统

  • 使用System.IO


  • 请使用以下代码调用RESTful web服务

    string responseMessage;
    HttpClient client = new HttpClient(serviceUrl);
    HttpWebRequest request = WebRequest.Create(string.Concat(serviceUrl, resourceUrl)) as HttpWebRequest;
    request.ContentType = "text/xml";
    request.Method = method;
    HttpContent objContent = HttpContentExtensions.CreateDataContract(requestBody);
    if(method == "POST" && requestBody != null)
    {
        //byte[] requestBodyBytes = ToByteArrayUsingXmlSer(requestBody, "http://schemas.datacontract.org/2004/07/XMLService");
        byte[] requestBodyBytes = ToByteArrayUsingDataContractSer(requestBody);
        request.ContentLength = requestBodyBytes.Length;
        using (Stream postStream = request.GetRequestStream())
            postStream.Write(requestBodyBytes, 0, requestBodyBytes.Length);
        //request.Timeout = 60000;
    }
    
    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
    if(response.StatusCode == HttpStatusCode.OK)
    {
        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);
    
        responseMessage = reader.ReadToEnd();
    }
    else
    {
        responseMessage = response.StatusDescription;
    }
    
    上述代码需要引用以下名称空间:

  • 使用Microsoft.Http;-->可从REST初学者工具包(Microsoft.Http.dll)获得

  • Net系统

  • 使用System.IO