2010-12-01 142 views
4

我试图连接到SalesForce.com批量API,因此我可以从我的应用程序批量上传数据。我已经通读了PDF documentation,它强调使用CURL来发出POST请求。按照说明,我创建了一个XML格式的文本文件,用于登录到服务器。下面使用C#连接到SalesForce批量API

的login.txt内容:

<?xml version="1.0" encoding="utf-8" ?> 
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> 
    <env:Body> 
    <n1:login xmlns:n1="urn:partner.soap.sforce.com"> 
      <n1:username>my username</n1:username> 
     <n1:password>my password</n1:password> 
    </n1:login> 
    </env:Body> 
</env:Envelope> 

这里是什么,我想在我的代码做,使登录请求:

XmlDocument XMLResponse = null; 

HttpWebRequest httpRequest; 

HttpWebResponse httpResponse = null; 

Stream requestStream = null; 
Stream responseStream = null; 

XmlTextReader xmlReader; 

httpRequest = HttpWebRequest)WebRequest.Create("https://login.salesforce.com/services/Soap/c/20.0"); 

try 
{ 
      byte[] bytes = File.ReadAllBytes(filename); 
      httpRequest.Method = "POST"; 
      httpRequest.ContentLength = bytes.Length; 
      httpRequest.ContentType = "text/xml; charset=UTF-8"; 
      httpRequest.Headers.Add("SOAPAction: login"); 
      requestStream = httpRequest.GetRequestStream(); 
      requestStream.Write(bytes, 0, bytes.Length); 
      requestStream.Close(); 

      httpResponse = (HttpWebResponse)httpRequest.GetResponse(); 

      if (httpResponse.StatusCode == HttpStatusCode.OK) 
      { 
       responseStream = httpResponse.GetResponseStream(); 

       xmlReader = new XmlTextReader(responseStream); 

       XmlDocument xmldoc = new XmlDocument(); 
       xmldoc.Load(xmlReader); 

       XMLResponse = xmldoc; 
       xmlReader.Close(); 
      } 

      httpResponse.Close(); 
} 

当这个代码执行我总是得到500错误。有没有人有任何经验来做我想做的事情?你能否给我提供一些建议?

预先感谢您。

回答

3

对于登录部分,只需下载并导入合作伙伴WSDL并使用生成的Web服务客户端。否则,您将需要更新代码以在获取500状态代码时读取响应(SOAP规范要求错误消息使用500状态代码),响应主体将为您提供更多线索。我希望在这种情况下,您会收到身份确认错误,并且除了登录请求中的密码之外,还需要提供您的api安全令牌。

+0

我怎样才能读取响应?只要.GetResponse()被调用,它就会将我的请求传给我的catch块,并使用通用500异常,它没有InnerException。 – Adam 2010-12-01 18:46:25

3

生成的XML文件到Salesforce登录

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <soap:Body> 
    <login xmlns="urn:enterprise.soap.sforce.com"> 
     <username>username</username> 
     <password>password + token</password> 
    </login> 
    </soap:Body> 
</soap:Envelope> 

使用下面的C#代码登录到Salesforce

XmlDocument doc = new XmlDocument(); 
       doc.LoadXml(str); 
       string uri = "https://login.salesforce.com/services/Soap/c/21.0"; 
       HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri); 
       req.Headers.Add("SOAPAction", "login"); 
       req.ContentType = "text/xml;charset=\"utf-8\""; 
       req.Accept = "text/xml"; 
       req.Method = "POST"; 
       stm = req.GetRequestStream(); 
       doc.Save(stm); 
       stm.Close(); 
       WebResponse resp = req.GetResponse(); 
       stm = resp.GetResponseStream(); 
       XmlDocument doc1 = new XmlDocument(); 
       doc1.Load(stm);