title: Bing API - Batch job of driving durations and distances
body: <p>I'm looking towards those with experience batching jobs using the Bing API. I have been able to batch addresses and return geocodes using sample code from Microsoft's <a href="/http://msdn.microsoft.com/en-us/library/ff701734.aspx">Bing Spatial Data Services</a> documentation, but haven't been able to batch 'Routes' in order to get driving durations between two addresses or two sets of geo locations. Below is the http query to get durations as a single query followed by the sample function to batch queries. </p>
  
  <p>Edit:
  I imagine that by swapping 'geocode' for 'Routes' in this snippet would be an intuitive way (if it is even supported in batch mode):</p>
  
  <pre><code>    //Build the HTTP URI that will upload and create the geocode dataflow job
      UriBuilder uriBuilder = new UriBuilder("http://spatial.virtualearth.net");
      uriBuilder.Path = "/REST/v1/dataflows/geocode";
      uriBuilder.Query = queryStringBuilder.ToString();
  </code></pre>
  
  <p>Thanks for any pointers, examples, links showing how to batch routes with the Bing API. (Note: A Bing key is required in order to run below URL.)</p>
  
  <p>Route URL:</p>
  
  <p><a href="http://dev.virtualearth.net/REST/V1/Routes/Driving?wp.0=Minneapolis,MN&amp;wp.1=St%20Paul,MN&amp;optmz=distance&amp;rpo=Points&amp;key=BingMapsKey" rel="nofollow">http://dev.virtualearth.net/REST/V1/Routes/Driving?wp.0=Minneapolis,MN&amp;wp.1=St%20Paul,MN&amp;optmz=distance&amp;rpo=Points&amp;key=BingMapsKey</a></p>
  
  <p>Batch Job for Address-to-Geocodes:</p>
  
  <pre><code>static string CreateJob(string dataFilePath, string dataFormat, string key, string description)
  {
          string contentType = "text/plain";
          if (dataFormat.Equals("xml", StringComparison.OrdinalIgnoreCase))
              contentType = "application/xml";
  
          StringBuilder queryStringBuilder = new StringBuilder();
  
          queryStringBuilder.Append("input=").Append(Uri.EscapeUriString(dataFormat));
          queryStringBuilder.Append("&amp;");
          queryStringBuilder.Append("key=").Append(Uri.EscapeUriString(key));
  
          if (!String.IsNullOrEmpty(description))
          {
              queryStringBuilder.Append("&amp;");
              queryStringBuilder.Append("description=").Append(Uri.EscapeUriString(description));
          }
  
          //Build the HTTP URI that will upload and create the geocode dataflow job
          UriBuilder uriBuilder = new UriBuilder("http://spatial.virtualearth.net");
          uriBuilder.Path = "/REST/v1/dataflows/geocode";
          uriBuilder.Query = queryStringBuilder.ToString();
  
          //Include the data to geocode in the HTTP request
          using (FileStream dataStream = File.OpenRead(dataFilePath))
          {
              HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uriBuilder.Uri);
  
              request.Method = "POST";
              request.ContentType = contentType;
  
              using (Stream requestStream = request.GetRequestStream())
              {
                  byte[] buffer = new byte[16384];
                  int bytesRead = dataStream.Read(buffer, 0, buffer.Length);
                  while (bytesRead &gt; 0)
                  {
                      requestStream.Write(buffer, 0, bytesRead);
  
                      bytesRead = dataStream.Read(buffer, 0, buffer.Length);
                  }
              }
  
              //Submit the HTTP request and check if the job was created successfully. 
              using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
              {
                  if (response.StatusCode != HttpStatusCode.Created)
                      throw new Exception("An HTTP error status code was encountered when creating the geocode job.");
  
                  string dataflowJobLocation = response.GetResponseHeader("Location");
                  if (String.IsNullOrEmpty(dataflowJobLocation))
                      throw new Exception("The 'Location' header is missing from the HTTP response when creating a goecode job.");
  
                  return dataflowJobLocation;
              }
          }
  }
  </code></pre>