Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I am trying to geocode a list of addresses to be flagged by a data structure to tell if students are part of a 10 mile radius of the campus or not. My problem is that I am not sure what the HttpWebResponse is doing or how to use the HttpWebResponse. When I run my program.cs file, the HttpWebResponse throws the exception that there is a "Bad Request 400". I am not sure how to fix this issue. I did not write this code. I am just editing and modifying to complete the project.

Thanx in advance for the help.

Here is the code that is throwing the error.

C#
//Submit the HTTP request and check if the job was created successfully.
               using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
               {
                   //
                   // If the job was created successfully, the status code should be
                   // 201 (Created) and the 'Location' header should contain a URL
                   // that defines the location of the new dataflow job. You use this
                   // URL with the Bing Maps Key to query the status of your job.
                   //
                   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;
Posted
Updated 12-Jun-14 3:17am
v2
Comments
Member 10880495 12-Jun-14 9:33am    
Here is the Program Code.

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Xml;

namespace GeocodeDataFlowExample
{
//A summary of status information returned in the response when you check
// job status.
class DownloadDetails
{
public string jobStatus { get; set; }
public string suceededlink { get; set; }
public string failedlink { get; set; }

}

class Program
{
//Creates a geocode dataflow job and uploads spatial data to process.
//Parameters:
// dataFilePath: The path to the file that contains the spatial data to geocode.
// dataFormat: The format of the input data. Possible values are xml, csv, tab and pipe.
// key: The Bing Maps Key to use for this job. The same key is used to get job status and download results.
// description: Text that is used to describe the geocode dataflow job.
//Return value : A URL that defines the location of the geocode dataflow job that was created.
static string CreateJob(string dataFilePath, string dataFormat, string key, string description)
{
//Define parameters for the HTTP request
//
// The 'Content-Type' header of the HTTP Request must be "text/plain" or "application/xml"
// depending on the input data format.
//
string contentType = "text/plain";
if (dataFormat.Equals("xml", StringComparison.OrdinalIgnoreCase))
contentType = "application/xml";

StringBuilder queryStringBuilder = new StringBuilder();

//
// The 'input'(input format) and 'key' (Bing Maps Key) parameters are required.
//
queryStringBuilder.Append("input=").Append(Uri.EscapeUriString(dataFormat));
queryStringBuilder.Append("&");
queryStringBuilder.Append("key=").Append(Uri.EscapeUriString(key));

if (!String.IsNullOrEmpty(description))
{
//
// The 'description' parameter is optional.
//
queryStringBuilder.Append("&");
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);

//
// The HTTP method must be 'POST'.
//
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 > 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 the job was created successfully, the status code should be
// 201 (Created) and the 'Location' header should contain a URL
// that defines the location of the new dataflow job. You use t
gggustafson 13-Jun-14 12:37pm    
If you are going to submit code as part of your question, place it in the question itself. If you are supplying code as an afterthought, use the Improve question button. As you can see, your code is unformatted and truncated by supplying it in a reply.
ZurdoDev 12-Jun-14 9:45am    
Look at the various properties of the response.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900