我们要想使用web api, 需要首先在azure 中创建application. (如何创建application可以参考我的另一篇blog 从O365中获取users到D365中 )

Get

我们可以用JObject 和 JArray 来快速获取而不需要DeserializeObject

         //server-side online oauth2
var sessionResult = (Opportunity)Session["OpportunityData"];
var httpUrl = resourceUrl + "api/data/v9.1/accounts?$filter=accountid%20eq%20" + "hello world";
AuthenticationContext authContext = new AuthenticationContext("https://login.windows.net/common", false);
AuthenticationResult result = authContext.AcquireToken(resourceUrl, clientId, new UserCredential(account, password));
using (HttpClient httpClient = new HttpClient())
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
httpClient.Timeout = new TimeSpan(, , ); // 2 minutes
httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", result.AccessToken);
httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0");
HttpResponseMessage response = httpClient.GetAsync(httpUrl).Result;
var returnvalue = response.Content.ReadAsStringAsync().Result;
LogHelper.WriteLog("Return value:");
LogHelper.WriteLog(returnvalue);
JObject jo = JsonConvert.DeserializeObject<JObject>(returnvalue);
JArray ja = JsonConvert.DeserializeObject<JArray>(jo["value"].ToString());
          }

POST:

ServicePointManager.SecurityProtocol 是必须要添加的. 不然会抛出 security 的exception.

req.Headers.Add("If-Match", "*"); 如果我们在请求的表头里添加了  if-match的话,  如果有相同的record创建时,会抛出 exception 412 error 而不会去创建一条一模一样的数据.

                var weburi = resourceUrl + "api/data/v9.1/accounts";
AuthenticationContext authContext = new AuthenticationContext("https://login.windows.net/common", false);
AuthenticationResult result = authContext.AcquireToken(resourceUrl, clientId, new UserCredential(account, password));
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(weburi); req.Method = "post";
req.Accept = "application/json";
req.ContentType = "application/json; charset=utf-8";
req.Headers.Add("OData-MaxVersion", "4.0");
req.Headers.Add("OData-Version", "4.0");
req.Headers.Add("If-Match","*");
req.Headers.Set("Authorization", "Bearer " + result.AccessToken);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; var newSurveyResult = new JObject();
newSurveyResult.Add("booleanvalue", true);
newSurveyResult.Add("stringvalue", "Hello World!");byte[] data = Encoding.UTF8.GetBytes(newSurveyResult.ToString());
Stream newStream = req.GetRequestStream();
newStream.Write(data, , data.Length);
newStream.Close();
using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
{
//StreamReader read = new StreamReader(res.GetResponseStream());
string head = res.Headers.ToString();
}

PATCH:

                var weburi = resourceUrl + "api/data/v9.1/accounts?$filter=accountid eq " + id;
AuthenticationContext authContext = new AuthenticationContext("https://login.windows.net/common", false);
AuthenticationResult result = authContext.AcquireToken(resourceUrl, clientId, new UserCredential(account, password));
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(weburi);
req.Method = "PATCH";
req.Accept = "application/json";
req.ContentType = "application/json; charset=utf-8";
req.Headers.Add("OData-MaxVersion", "4.0");
req.Headers.Add("OData-Version", "4.0"); req.Headers.Set("Authorization", "Bearer " + result.AccessToken);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; var newSurveyResult = new JObject
{
{"stringvalue", "Hello World"},
{"intvalue", 123 },
{ "booleanvalue" , true}
}; byte[] data = Encoding.UTF8.GetBytes(newSurveyResult.ToString());
Stream newStream = req.GetRequestStream();
newStream.Write(data, , data.Length);
newStream.Close();
using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
{
StreamReader read = new StreamReader(res.GetResponseStream());
}

在后端C#中 call web api的更多相关文章

  1. 购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(3)--Idetity,OWIN前后端验证

    原文:购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(3)--Idetity,OWIN前后端验证 chsakell分享了前端使用AngularJS,后端使用ASP. ...

  2. 购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(2)--前端,以及前后端Session

    原文:购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(2)--前端,以及前后端Session chsakell分享了前端使用AngularJS,后端使用ASP.NE ...

  3. 购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(1)--后端

    原文:购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(1)--后端 chsakell分享了前端使用AngularJS,后端使用ASP.NET Web API的购物车 ...

  4. 对一个前端AngularJS,后端OData,ASP.NET Web API案例的理解

    依然chsakell,他写了一篇前端AngularJS,后端OData,ASP.NET Web API的Demo,关于OData在ASP.NET Web API中的正删改查没有什么特别之处,但在前端调 ...

  5. 对一个前端使用AngularJS后端使用ASP.NET Web API项目的理解(4)

    chsakell分享了一个前端使用AngularJS,后端使用ASP.NET Web API的项目. 源码: https://github.com/chsakell/spa-webapi-angula ...

  6. 对一个前端使用AngularJS后端使用ASP.NET Web API项目的理解(3)

    chsakell分享了一个前端使用AngularJS,后端使用ASP.NET Web API的项目. 源码: https://github.com/chsakell/spa-webapi-angula ...

  7. 对一个前端使用AngularJS后端使用ASP.NET Web API项目的理解(2)

    chsakell分享了一个前端使用AngularJS,后端使用ASP.NET Web API的项目. 源码: https://github.com/chsakell/spa-webapi-angula ...

  8. 对一个前端使用AngularJS后端使用ASP.NET Web API项目的理解(1)

    chsakell分享了一个前端使用AngularJS,后端使用ASP.NET Web API的项目. 源码: https://github.com/chsakell/spa-webapi-angula ...

  9. ASP.NET 5系列教程 (六): 在 MVC6 中创建 Web API

    ASP.NET 5.0 的主要目标之一是统一MVC 和 Web API 框架应用. 接下来几篇文章中您会了解以下内容: ASP.NET MVC 6 中创建简单的web API. 如何从空的项目模板中启 ...

随机推荐

  1. WebApi发布IIS404

    1.确定访问路径有没有问题,路由的设置: 2.配置文件中要加 <modules runAllManagedModulesForAllRequests="true" /> ...

  2. springboot - 映射HTTP Response Status Codes 到 FreeMarker Error页面

    1.总览 2.代码 1).pom.xml 这里注意:springboot 2.2.0以后默认的freemarker文件后缀为:ftlh.本例用的是2.2.1,所以后缀为ftlh <depende ...

  3. P1031 查验身份证

    转跳点:

  4. redis qps监控

    最近有个客户反应redis的qps不准确,通过查看代码,发现我们的监控是调用的redis info里面的instantaneous_ops_per_sec 客户反应说自己压测了ns,但是从监控上看不到 ...

  5. PHP四种输出语句

    //echo 深入理解echo ,echo是一个函数 //echo 功能:向浏览器输出一个或多个字符串; //echo 返回值:void 无返回值; echo "今天是个好天气"; ...

  6. GCPC 2013_A Boggle DFS+字典树 CSU 1457

    上周比赛的题目,由于那个B题被神编译器的优化功能给卡了,就没动过这个题,其实就是个字典树嘛.当然,由于要在Boggle矩阵里得到初始序列,我还一度有点虚,不知道是用BFS还是DFS,最后发现DFS要好 ...

  7. 编写检测深度模型测试程序python

    参考:https://blog.csdn.net/haoji007/article/details/81035565?utm_source=blogxgwz9 首先从网上下载imagenet训练好的模 ...

  8. unix中嘚vim编辑器

    在linux家族中,vim编辑器是系统自带的文本编辑器,其功能强大自不必说了. 偶有小白,刚接触linux,要修改某个文本文件,不可能像WINDOWS那样操作,更有甚者,进入VI编辑器后,无法退出以致 ...

  9. HTML5中的行级标签和块级标签

    行级标签 1.行级标签又称为内联标签,行级标签不会单独占据一行,设置宽高无效. 2.行内内部可以容纳其他行内元素,但不可以容纳块元素.有span.strong.em.b.i.input.a.img.u ...

  10. Python说文解字_Python之多任务_02

    第三部分:Semaphore控制进入数量的锁 有时候可能需要运行多个工作线程同时访问一个资源,但要限制总数.例如,连接池支持同时连接,但是数目可能是固定的,或者一个网络应用可能支持固定数据的并发下载. ...