我是Dynamics CRM Web服务的新手,我想从控制台应用程序使用Web服务,它从我的本地工作,但是当它转到生产服务器时,它不起作用,因为生产服务器在调用Web服务之前需要设置代理。有人可以在调用Web服务之前解释如何使用代理,这是在我的本地机器中工作的代码
private static void ConnectToDynamics()
{
string odataUrl = _dynamicsURL;
string appId = Id;
string clientSecret = _secret;
AuthenticationParameters authArg = AuthenticationParameters.CreateFromResourceUrlAsync(new Uri(odataUrl)).Result;
AuthenticationContext authCtx = new AuthenticationContext(authArg.Authority);
AuthenticationResult authRes = authCtx.AcquireTokenAsync(authArg.Resource, new ClientCredential(appId, clientSecret)).Result;
using (OrganizationWebProxyClient webProxyClient = new OrganizationWebProxyClient(new Uri(odataUrl), false))
{
webProxyClient.HeaderToken = authRes.AccessToken;
using (OrganizationServiceContext ctx = new OrganizationServiceContext((IOrganizationService)webProxyClient))
{
var accounts = (from i in ctx.CreateQuery("entity") orderby i["name"] select i).Take(100);
foreach (var account in accounts)
Console.WriteLine(account["name"]);
}
}
}
下面是
HttpWebResponse
的示例代码
ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
WebProxy proxy = new WebProxy("proxy.xyz.local", 81) { UseDefaultCredentials = true };
WebRequest request = WebRequest.Create(globaConfigStatciValues.Url);
request.Proxy = proxy;
request.Method = "GET";
request.Credentials = new NetworkCredential(globaConfigStatciValues.userName,
globaConfigStatciValues.Password);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
try
{
if (response.StatusCode == HttpStatusCode.OK)
{
isConnectionSucessfull = true;
tracing.Trace($" Resposne is correct {response.StatusCode}");
TracingMessage += $"Resposne is correct {response.StatusCode} n";
}
else
{
TracingMessage += $"Response from connecting to API {response.StatusCode} n";
tracing.Trace($"Response from connecting to API {response.StatusCode}");
}
}
catch (Exception e)
{
TracingMessage += $" In catch block {e} n";
tracing.Trace($" In catch block {e}");
createLogRecord( e.StackTrace,TracingMessage);
// throw new Exception($"There was an issue with connecting to API {e.Message}");
}
}