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


    基本类,拷贝下来直接调用就行:

    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,具体使用什么,看需求吧
    public string PostData { get; set; } //传送的数据,当然了我使用的是json字符串

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

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


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

    public string MakeRequest(string parameters)
    {
    // Uri uri = new Uri("http://localhost:8087/water-datacenter/dataTheme/historyData/historyData/getHisDataByIdsName?ids=1&startTime=1989-12-31 00:00:00&endTime=2000-12-31 00:00:00&rtName=yyzcb&rtName=zfgxsf");

    // var request = (HttpWebRequest)WebRequest.Create(uri);


    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
    var client = new RestClient();
    string endPoint = @"http:\myRestService.comapi";
    var client = new RestClient(endPoint);
    var json = client.MakeRequest();
    */

    var client = new RestClient();
    // client.EndPoint = @"http:\myRestService.comapi"; ;

    client.EndPoint = @"http://localhost:8087/water-datacenter/dataTheme/historyData/historyData/getHisDataByIdsName"; ;
    // http://localhost:8087/water-datacenter/dataTheme/historyData/historyData/getHisDataByIdsName?ids=1&startTime=1989-12-31 00:00:00&endTime=2000-12-31 00:00:00&rtName=yyzcb&rtName=zfgxsf
    client.ContentType = "application/json";
    client.Method = HttpVerb.POST;
    client.PostData = "{postData: value}";
    // var json = client.MakeRequest();
    var json = client.MakeRequest("?ids=1&startTime=1989-12-31 00:0:00&endTime=2000-12-31 00:00:00&rtName=yyzcb&rtName=zfgxsf"); //带参数

  • 相关阅读:
    pandas 筛选指定行或者列的数据
    数据相关性分析方法
    导入sklearn 报错,找不到相关模块
    特征探索经验
    python 中hive 取日期时间的方法
    云从科技 OCR任务 pixel-anchor 方法
    五种实现左中右自适应布局方法
    vscode vue 代码提示
    js Object.create 初探
    webpack 加载css 相对路径 ~
  • 原文地址:https://www.cnblogs.com/lj821022/p/5778373.html
Copyright © 2020-2023  润新知