使用C#连接到SalesForce批量API

时间:2010-12-01 16:34:48

标签: c# salesforce

我正在尝试连接到SalesForce.com批量API,以便我可以从我的应用程序大量上传数据。我已经阅读了PDF documentation,它强调使用CURL来发出POST请求。为了与说明保持一致,我创建了一个XML格式的文本文件,用于登录服务器。

下面的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错误。有没有人有任何经验可以做我想做的事情?你能给我一些建议吗?

提前谢谢。

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分: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);