博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c#后台代码请求访问api接口
阅读量:4705 次
发布时间:2019-06-10

本文共 3407 字,大约阅读时间需要 11 分钟。

      前言:最近公司项目与外部api接口对接较多 ,写下自己的代码总结。介绍两种访问方式(HttpClient、HttpWebRequest)

一、HttpWebRequest 访问Api

private static string webPost(string url, string obj = null)        {            string param = (obj);//参数            byte[] bs = Encoding.Default.GetBytes(param);            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);            req.Method = "POST";            req.ContentType = "application/json";            req.ContentLength = bs.Length;            using (Stream reqStream = req.GetRequestStream())            {                reqStream.Write(bs, 0, bs.Length);                reqStream.Close();                HttpWebResponse response2 = (HttpWebResponse)req.GetResponse();                StreamReader sr2 = new StreamReader(response2.GetResponseStream(), Encoding.UTF8);                string result = sr2.ReadToEnd();                return result;            }        }

 

二、HttpClient访问Api

///  /// Get请求 ///  public string GetToken()
{
try            {                HttpClient myHttpClient = new HttpClient();                var urlstring = "https://oapi.dingtalk.com/gettoken?appkey=APPKEY&appsecret=APPSECRET";                urlstring = urlstring.Replace("APPKEY", appkey);                urlstring = urlstring.Replace("APPSECRET", appsecret);                HttpResponseMessage response = myHttpClient.GetAsync(urlstring).Result;                var content = response.Content.ReadAsAsync().Result;                return content;            }            catch (Exception ex)            {                return ex.Message;            }}

 

/// /// Post请求///  public string getList(long unixstart, long unixend, int cursor, bool start) {            try            {                HttpClient myHttpClient = new HttpClient();                var urlstring = "https://oapi.dingtalk.com/topapi/processinstance/listids?access_token=ACCESS_TOKEN";                urlstring = urlstring.Replace("ACCESS_TOKEN", access_token);                var content = new FormUrlEncodedContent(new Dictionary
() { {
"process_code",""}, {
"start_time", }, {
"end_time" ,}, {
"size" ,"10"}, {
"cursor" ,)} }); HttpResponseMessage response = myHttpClient.PostAsync(urlstring, content).Result; var jsonlist = response.Content.ReadAsAsync
().Result; return jsonlist; } catch (Exception ex) { return e.Message; } }

 

三、时间戳与DateTime互转

///         /// 将c# DateTime时间格式转换为Unix时间戳格式        ///         ///         /// 
public long ConvertDateTimeInt(DateTime time) { DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0)); long t = (time.Ticks - startTime.Ticks) / 10000; return t; } /// /// c# Unix时间戳格式转换成DateTime /// /// ///
public System.DateTime ConvertIntDateTime(long d) { System.DateTime time = System.DateTime.MinValue; System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); time = startTime.AddMilliseconds(d); return time; }

 

转载于:https://www.cnblogs.com/wufanJY/p/10630273.html

你可能感兴趣的文章
CSS 文字溢出 变成省略号 ...
查看>>
Spring事务
查看>>
java编程基础(三)流程控制语句
查看>>
让数据库跑的更快的7个MySQL优化建议
查看>>
jquery 取id模糊查询
查看>>
解决在vue中,自用mask模态框出来后,下层的元素依旧可以滑动的问题
查看>>
PAT(B) 1014 福尔摩斯的约会(Java)
查看>>
PAT甲级题解-1123. Is It a Complete AVL Tree (30)-AVL树+满二叉树
查看>>
项目开发总结报告(GB8567——88)
查看>>
SSH加固
查看>>
端口扫描base
查看>>
iOS IM开发的一些开源、框架和教程等资料
查看>>
FansUnion:共同写博客计划终究还是“流产”了
查看>>
python 二维字典
查看>>
Arrays类学习笔记
查看>>
实验吧之【天下武功唯快不破】
查看>>
2019-3-25多线程的同步与互斥(互斥锁、条件变量、读写锁、自旋锁、信号量)...
查看>>
win7-64 mysql的安装
查看>>
dcm4chee 修改默认(0002,0013) ImplementationVersionName
查看>>
maven3在eclipse3.4.2中创建java web项目
查看>>