C#如何使用REST接口读写数据

原网站:http://www.codeproject.com/Tips/497123/How-to-make-REST-requests-with-Csharp

一个类,我们拷贝下来直接调用就行:

using System;
using System.IO;
using System.Net;
using System.Text;

public enum HttpVerb
{
    GET,            //method  常用的就这几样,当然你也可以添加其他的   get:获取    post:修改    put:写入    delete:删除
    POST,
    PUT,
    DELETE
}

namespace HttpUtils
{
  public class RestClient
  {
    public string EndPoint { get; set; }    //请求的url地址  eg:   http://215.23.12.45:8080/order/order_id=1&isdel=0
    public HttpVerb Method { get; set; }    //请求的方法
    public string ContentType { get; set; } //格式类型:我用的是application/json,text/xml具体使用什么,看需求吧
    public string PostData { get; set; }    //传送的数据,当然了我使用的是json字符串

    public RestClient()
    {
      EndPoint = "";
      Method = HttpVerb.GET;
      ContentType = "application/json";
      PostData = "";
    }
    public RestClient(string endpoint)
    {
      EndPoint = endpoint;
      Method = HttpVerb.GET;
      ContentType = "application/json";
      PostData = "";
    }
    public RestClient(string endpoint, HttpVerb method)
    {
      EndPoint = endpoint;
      Method = method;
      ContentType = "application/json";
      PostData = "";
    }

    public RestClient(string endpoint, HttpVerb method, string postData)
    {
      EndPoint = endpoint;
      Method = method;
      ContentType = "application/json";
      PostData = postData;
    }


    public string MakeRequest()
    {
      return MakeRequest("");
    }

    public string MakeRequest(string parameters)
    {
      var request = (HttpWebRequest)WebRequest.Create(EndPoint + parameters);

      request.Method = Method.ToString();
      request.ContentLength = 0;
      request.ContentType = ContentType;

      if (!string.IsNullOrEmpty(PostData) && Method == HttpVerb.POST)//如果传送的数据不为空,并且方法是post
      {
        var encoding = new UTF8Encoding();
        var bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(PostData);//编码方式按自己需求进行更改,我在项目中使用的是UTF-8
        request.ContentLength = bytes.Length;

        using (var writeStream = request.GetRequestStream())
        {
          writeStream.Write(bytes, 0, bytes.Length);
        }
      }

      if (!string.IsNullOrEmpty(PostData) && Method == HttpVerb.PUT)//如果传送的数据不为空,并且方法是put
      {
        var encoding = new UTF8Encoding();
        var bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(PostData);//编码方式按自己需求进行更改,我在项目中使用的是UTF-8
        request.ContentLength = bytes.Length;

        using (var writeStream = request.GetRequestStream())
        {
          writeStream.Write(bytes, 0, bytes.Length);
        }
      }
      using (var response = (HttpWebResponse)request.GetResponse())
      {
        var responseValue = string.Empty;

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

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

        return responseValue;
      }
    }

  } // class

}

  

 

以上是类,我演示一下使用的方法:

1,基本的调用:

 
  1. var client = new RestClient();  
  2. string endPoint = @"http:\\myRestService.com\api\";  
  3. var client = new RestClient(endPoint);  
  4. var json = client.MakeRequest();  

2,如果你想带入参数

 
  1. var json = client.MakeRequest("?param=0");  


3,使用最多的方式

  1. var client = new RestClient();  
  2. client.EndPoint = @"http:\\myRestService.com\api\"; ;  
  3. client.ContentType = "application/json";  
  4. client.Method = HttpVerb.POST;  
  5. client.PostData = "{postData: value}";  
  6. var json = client.MakeRequest();  


一般情况,MakeRequest返回的json格式的字符串(不排除有的接口开发商返回xml,html),我们可以把他转化为json对象,然后通过C#代码操控json对象,就会方便的很。当然jquery也很方便。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值