0
votes

I wants to get session id from salesforces using soap API for login. Below is my code for request but i am getting salesforce login page as html response. Any thoughts ?

public static string Authentication()
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(SalesForce_authenticationAPI);
        request.ContentType = "text/xml; charset=UTF-8";
        request.Headers.Add("SOAPAction", @"\");
        request.Method = "POST";

        byte[] bytes;
        bytes = System.Text.Encoding.ASCII.GetBytes(CreateSoapEnvelope().InnerXml);
        request.ContentLength = bytes.Length;           

        Stream requestStream = request.GetRequestStream();
        requestStream.Write(bytes, 0, bytes.Length);

        requestStream.Close();

        HttpWebResponse response;
        response = (HttpWebResponse)request.GetResponse();
        if (response.StatusCode == HttpStatusCode.OK)
        {
            Stream responseStream = response.GetResponseStream();
            string responseStr = new StreamReader(responseStream).ReadToEnd();
            //return only sessionId
            return responseStr;
        }
        return null;

    }


private static XmlDocument CreateSoapEnvelope()
    {
        XmlDocument soapEnvelop = new XmlDocument();
        soapEnvelop.LoadXml(@"<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/' xmlns:x='http://www.w3.org/2001/XMLSchema-instance' xmlns='urn:partner.soap.sforce.com'><s:Body><login><username>[email protected]</username><password>*********</password></login></s:Body></s:Envelope>");
        return soapEnvelop;

    }
1

1 Answers

1
votes

Rather than doing the SOAP stuff by hand, check out the Force.com-Toolkit-for-NET.

I don't have a .Net sample but here is a Scala example that uses the Salesforce SOAP API to login: https://github.com/jamesward/force-login