我们要想使用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. python---生成式

    1.[(x,y) for x in [1,2,3] for y in [4,2,3] if x == y] (x,y):输出表达式,产生最终列表的元素 for x in [1,2,3] for y i ...

  2. python_re正则表达

    re模块就本质而言,正则表达式(或RE)是一种小型的.高度专业化的编程语言,(在python中)它内嵌在Python中,并通过re模块实现,正则表达式模块被编译成一系列的字节码,然后由用C编写的匹配引 ...

  3. P 1022 D进制的A+B

    转跳点 :

  4. 【golang】golang文本处理

    golang文本字符串操作:包含 合并 连接 分割 取索引 前缀后缀检测 消除字符串 消除空格 golang字符串操作需要用到 strings这个包 str := "hello world& ...

  5. 由于TableView的Section的头部和尾部高度设置的不规范引起的部分Section中的图片无法正常显示

    当tableview的组的头部和尾部的高度设置如下时: -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(N ...

  6. Nginx系列p4:进程结构

    Nginx 有两种进程结构:单进程结构,多进程结构.本篇文章我们主要说多进程结构. 问:那为什么 Nginx 采用多进程结构,而不是多线程结构呢? 答:这是因为 Nginx 最核心的目的就是要保证高可 ...

  7. idea排除要编译的文件

    感觉应该有更好的方式.VS中可以右键文件从项目中排除 如果要恢复选中要恢复的文件,点击 +下的-即可

  8. Codeforces 1299B/1300D - Aerodynamic

    题目大意: 给定一个图形S,让这个图形任意平移,但是要保证原点(0,0)一直在它的内部或者边上 最后把它能移动到的所有位置进行拼合可以得到一个图形T 问图形S与图形T是否相似 点会按照逆时针顺序给出 ...

  9. XML--XML Schema Definition(一)

    参考 https://blog.csdn.net/wangw2008/article/details/83195283 https://blog.csdn.net/lmj623565791/artic ...

  10. A4纸网页打印

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...