最新消息:ww12345678 的部落格重装上线,希望大家继续支持。

使用X ++中的HTTP基本身份验证调用REST Webservice / Call REST Webservice with HTTP Basic Authentication from X++

网络文摘 William 298浏览 0评论
Calling a web service with HTTP Basic Authentication is easy in C#. Here I’m using a REST service via HTTP GET which is secured via Basic Authentication.

C# Code

A HTTP GET webservice call using System.Net.WebRequest and System.Net.Webrespose in C#
string url = “http://yourhost.net/service”; string user = “YourUserName”; string pass = “YourPassWord”; byte[] bytes = System.Text.Encoding.UTF8.GetBytes(user+”:”+ pass); string base64  = System.Convert.ToBase64String(bytes); WebRequest request = WebRequest.Create(url); request.Headers.Add(“Authorization”, “Basic “+base64); WebResponse response = request.GetResponse(); string wsResponse = new StreamReader(response.GetResponseStream()) .ReadToEnd(); Console.WriteLine(wsResponse); Console.ReadKey();

X++ Code

Since Dynamics 365 FO does not support packages and some syntactic sugar from C# the code is more wordy.
str url = “http://yourhost.net/service”; str user = “YourUserName”; str pass = “YourPassword”; System.Text.Encoding encoding = System.Text.Encoding::UTF8; System.Byte[] bytes = encoding.GetBytes(user + “:” + pass); System.String base64 = System.Convert::ToBase64String(bytes); System.String headerKey = “Authorization”; System.String headerValue = “Basic ” + base64; System.Net.WebRequest request = System.Net.WebRequest::Create(url); System.Net.WebHeaderCollection headers = request.Headers; headers.Add(headerKey,headerValue); System.Net.WebResponse response = request.GetResponse(); System.IO.StreamReader reader = new System.IO.StreamReader( response.GetResponseStream()); str wsResponse = reader.ReadToEnd(); info(wsResponse);
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址