C# 如何从基本CRM API身份验证获取响应

C# 如何从基本CRM API身份验证获取响应,c#,api,authentication,base,C#,Api,Authentication,Base,我正试图从基本API获得响应,但我一直收到“500内部服务器错误”错误。我希望得到至少401HTTP响应,这意味着身份验证调用失败。以下是使用基本API身份验证的说明: 这是我的代码: public string Authenticate() { string result = ""; string url = "https://sales.futuresimple.com/api/v1/"; string

我正试图从基本API获得响应,但我一直收到“500内部服务器错误”错误。我希望得到至少401HTTP响应,这意味着身份验证调用失败。以下是使用基本API身份验证的说明:

这是我的代码:

public string Authenticate()
        {
            string result = "";
            string url = "https://sales.futuresimple.com/api/v1/";
            string email = "mail@mail.com";
            string password = "pass";
            string postData = "email=" + email + "&password=" + password;
            HttpWebRequest request = null;
            Uri uri = new Uri(url + "authentication.xml");
            request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = "POST";
            request.ContentType = "application/xml";
            request.ContentLength = postData.Length;

            using (Stream writeStream = request.GetRequestStream())
            {
                UTF8Encoding encoding = new UTF8Encoding();
                byte[] bytes = encoding.GetBytes(postData);
                writeStream.Write(bytes, 0, bytes.Length);
                writeStream.Close();
            }

            try
            {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8))
                        {
                            result = readStream.ReadToEnd();
                        }
                    }
                }
            }
            catch (WebException ex)
            {
                ex = ex;
            }
            return result;
        }

您正在将
ContentType
设置为
application/xml
——这是请求主体的类型。您发送的正文(
string postData=“email=“+email+”&password=“+password;”
)是表单编码的,而不是xml。只需跳过行
request.ContentType=“application/xml”应该可以做到这一点。或者,您可以将请求正文编码为xml。

您正在将
ContentType
设置为
application/xml
——这是请求正文的类型。您发送的正文(
string postData=“email=“+email+”&password=“+password;”
)是表单编码的,而不是xml。只需跳过行
request.ContentType=“application/xml”应该可以做到这一点。或者,您可以将请求主体编码为xml