Yahoo Placefinderサービスを使用して、csvファイルにある住所のリストの緯度/経度の位置を検索しています。
私は次のコードを使用しています:
String reqURL = "http://where.yahooapis.com/geocode?location=" + HttpUtility.UrlEncode(location) + "&appid=KGe6P34c";
XmlDocument xml = new XmlDocument();
xml.Load(reqURL);
XPathNavigator nav = xml.CreateNavigator();
// process xml here...
私は非常に頑固なエラーを見つけました。それは、Yahooが私からのそれ以上のリクエストを禁止したために(誤って)数日間考えたものです。
このURL用です:
http://where.yahooapis.com/geocode?location=31+Front+Street%2c+Sedgefield%2c+Stockton%06on-Tees%2c+England%2c+TS21+3AT&appid=KGe6P34c
私のブラウザは、そのURLの解析エラーについて文句を言います。私のc#プログラムは500エラーがあると言っています。
ここでのロケーション文字列は、次のアドレスから取得されます。
Agape Business Consortium Ltd.,michael.cutbill@agapesolutions.co.uk,Michael A Cutbill,Director,,,9 Jenner Drive,Victoria Gardens,,Stockton-on-Tee,,TS19 8RE,,England,85111,Hospitals,www.agapesolutions.co.uk
エラーはの最初のハイフンに起因すると思いますが、Stockton-on-Tee
これがなぜであるかを説明することはできません。このハイフンを「通常の」ハイフンに置き換えると、クエリは正常に実行されます。
このエラーは、私の側の障害(HttpUtility.UrlEncode
機能が正しくないのですか?)またはYahooの側の障害によるものですか?
この問題の原因はわかりますが、理由はわかりません。誰かが説明できますか?
編集:
私の側でさらに調査すると、このハイフンがエンコードされている文字「%06」は、ASCII制御文字「Acknowledge」「ACK」であることがわかります。なぜこのキャラクターがここに現れるのか私にはわかりません。さまざまな場所がさまざまな方法でレンダリングされるようですStockton-on-Tee
。テキストエディタで通常どおり開いているように見えますが、Visual Studioに表示されるまでに、エンコードされる前はですStocktonon-Tees
。Firefoxのこのテキストボックスに前のテキストをコピーすると、ハイフンが奇妙な四角いボックスの文字としてレンダリングされましたが、この後続の編集では、SOソフトウェアが文字をサンティゼーションしたように見えることに注意してください。
csvファイルの解析に使用している関数とホルダークラスを以下に示します。ご覧のとおり、予期しない文字が発生する可能性のある奇妙なことは何もしていません。危険なキャラクターが「タウン」フィールドに表示されます。
public List<PaidBusiness> parseCSV(string path)
{
List<PaidBusiness> parsedBusiness = new List<PaidBusiness>();
List<string> parsedBusinessNames = new List<string>();
try
{
using (StreamReader readFile = new StreamReader(path))
{
string line;
string[] row;
bool first = true;
while ((line = readFile.ReadLine()) != null)
{
if (first)
first = false;
else
{
row = line.Split(',');
PaidBusiness business = new PaidBusiness(row);
if (!business.bad) // no problems with the formatting of the business (no missing fields, etc)
{
if (!parsedBusinessNames.Contains(business.CompanyName))
{
parsedBusinessNames.Add(business.CompanyName);
parsedBusiness.Add(business);
}
}
}
}
}
}
catch (Exception e)
{ }
return parsedBusiness;
}
public class PaidBusiness
{
public String CompanyName, EmailAddress, ContactFullName, Address, Address2, Address3, Town, County, Postcode, Region, Country, BusinessCategory, WebAddress;
public String latitude, longitude;
public bool bad;
public static int noCategoryCount = 0;
public static int badCount = 0;
public PaidBusiness(String[] parts)
{
bad = false;
for (int i = 0; i < parts.Length; i++)
{
parts[i] = parts[i].Replace("pithawala", ",");
parts[i] = parts[i].Replace("''", "'");
}
CompanyName = parts[0].Trim();
EmailAddress = parts[1].Trim();
ContactFullName = parts[2].Trim();
Address = parts[6].Trim();
Address2 = parts[7].Trim();
Address3 = parts[8].Trim();
Town = parts[9].Trim();
County = parts[10].Trim();
Postcode = parts[11].Trim();
Region = parts[12].Trim();
Country = parts[13].Trim();
BusinessCategory = parts[15].Trim();
WebAddress = parts[16].Trim();
// data testing
if (CompanyName == "")
bad = true;
if (EmailAddress == "")
bad = true;
if (Postcode == "")
bad = true;
if (Country == "")
bad = true;
if (BusinessCategory == "")
bad = true;
if (Address.ToLower().StartsWith("po box"))
bad = true;
// its ok if there is no contact name.
if (ContactFullName == "")
ContactFullName = CompanyName;
//problem if there is no business category.
if (BusinessCategory == "")
noCategoryCount++;
if (bad)
badCount++;
}
}