2013-02-04 26 views
1

我试图从Base API获得响应,但我一直收到“500 Internal Server Error”错误。我想获得至少401个HTTP响应,这意味着认证呼叫失败。下面是使用基本API认证的描述:如何从Base CRM API身份验证获得响应

http://dev.futuresimple.com/api/authentication

这里是我的代码:

public string Authenticate() 
     { 
      string result = ""; 
      string url = "https://sales.futuresimple.com/api/v1/"; 
      string email = "[email protected]"; 
      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; 
     } 

回答

1

你设置ContentTypeapplication/xml - 这是请求主体的类型。您发送的正文(string postData = "email=" + email + "&password=" + password;)是表单编码而不是xml。只要跳过request.ContentType = "application/xml";就行。或者,您可以将您的请求主体编码为xml。