• ggMan
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 3
    Replies

Hello,

 

I'm trying to add a batch to an existing job using the Bulk API from C#.  I've created the job and can get info on it, but am running into a problem when attempting to add a batch of data to it.

 

Here's the code.  The problem is that at run time the call to this line never returns:

Stream datastream = request.GetRequestStream();

 

This only happens when the url on the request includes the "batch" at the end (see first line of code).  The JobID parameter is valid, is an open job, and works in a separate call to get info.

 

Does anyone know what the problem might be?

 

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://na12-api.salesforce.com/services/async/23.0/job/" + JobID + "/batch");
            request.Method = WebRequestMethods.Http.Post;
            request.ContentType = "application/xml; charset=UTF-8";
            request.Headers.Add("X-SFDC-Session", _SessionID);
            request.KeepAlive = false;
            request.Host = "na12.salesforce.com";
            request.UserAgent = ".NET Framework Test Client";
            request.Accept = "application/xml";

            string body = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> ";
            body += "<sObjects xmlns=\"http://www.force.com/2009/06/asyncapi/dataload\">";
            body += "  <sObject>";
            body += "    <description>Created from Bulk API on Wed Jan 4 2012</description>";
            body += "    <name>[Bulk API] Account 0 (batch 0)</name>";
            body += "  </sObject>";
            body += "  <sObject>";
            body += "    <description>Created from Bulk API on Wed Jan 4 2012</description>";
            body += "    <name>[Bulk API] Account 1 (batch 0)</name>";
            body += "  </sObject>";
            body += "</sObjects>";

            //Convert the body of request into a byte array
            byte[] byteArray = Encoding.UTF8.GetBytes(body);

            //Set the length
            request.ContentLength = byteArray.Length;

            //Write the body to the request by using a datastream
            //This line never returns....
            Stream datastream = request.GetRequestStream();
            datastream.Write(byteArray, 0, byteArray.Length);
            datastream.Close();

            //Call the service and get the response as a data stream
            WebResponse response = request.GetResponse();
            datastream = response.GetResponseStream();
            StreamReader reader = new StreamReader(datastream);
            string responseFromServer = reader.ReadToEnd();

            response.Close();