Web请求错误 - 目标主体名称不正确

时间:2017-12-05 10:25:05

标签: c# rest dynamic dynamics-crm

我正在尝试向我们的crm发送webrequest。 当我使用浏览器访问URL时,一切正常,我得到了回复。 但是根据我的c#请求,我收到以下错误。

  

远程服务器返回错误:(401)未经授权。目标   主要名称不正确。

代码看起来像这样

String url = @"http://localhost/CRMOrga";
String parameters = @"/api/data/v8.2/contacts?$select=lastname,e_mail&$filter=lastname eq 'TEST'";

var request = (HttpWebRequest)WebRequest.Create(url + parameters);

request.Method = "GET";
request.ContentLength = 0;
request.ContentType = "application/json";
request.Credentials = new NetworkCredential(@"DOMAIN\USERNAME", "PASSWORD");

using (var response = (HttpWebResponse)request.GetResponse())
{
    String responseValue = string.Empty;

    if (response.StatusCode != HttpStatusCode.OK)
    {
        var message = String.Format("Request failed. Received HTTP {0}", response.StatusCode);
        throw new ApplicationException(message);
    }

    // grab the response
    using (var responseStream = response.GetResponseStream())
    {
        if (responseStream != null)
            using (var reader = new StreamReader(responseStream))
            {
                responseValue = reader.ReadToEnd();
            }
    }
    return responseValue;
}

0 个答案:

没有答案