咱們要想使用web api, 須要首先在azure 中建立application. (如何建立application能夠參考個人另外一篇blog 從O365中獲取users到D365中 )html
Getweb
咱們能夠用JObject 和 JArray 來快速獲取而不須要DeserializeObjectjson
//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(0, 2, 0); // 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:windows
ServicePointManager.SecurityProtocol 是必需要添加的. 否則會拋出 security 的exception.api
req.Headers.Add("If-Match", "*"); 若是咱們在請求的表頭裏添加了 if-match的話, 若是有相同的record建立時,會拋出 exception 412 error 而不會去建立一條如出一轍的數據.session
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, 0, data.Length); newStream.Close(); using (HttpWebResponse res = (HttpWebResponse)req.GetResponse()) { //StreamReader read = new StreamReader(res.GetResponseStream()); string head = res.Headers.ToString(); }
PATCH:app
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, 0, data.Length); newStream.Close(); using (HttpWebResponse res = (HttpWebResponse)req.GetResponse()) { StreamReader read = new StreamReader(res.GetResponseStream()); }