3

Delphi XE2を使用して最初のUnicodeアプリケーションを作成していGETますが、UnicodeURLへの要求に関する問題に遭遇しました。

つまり、これはMP3タグ付けアプリケーションのルーチンであり、トラックのタイトルとアーティストを取得し、Last.FMに対応するアルバム、トラック番号、およびジャンルを照会します。

私は次のコードを持っています:

function GetMP3Info(artist, track: string) : TMP3Data //<---(This is a record)
var
  TrackTitle,
  ArtistTitle : WideString;
  webquery    : WideString;

[....]

WebQuery := UTF8Encode('http://ws.audioscrobbler.com/2.0/?method=track.getcorrection&api_key=' + apikey + '&artist=' + artist + '&track=' + track);

//[processing the result in the web query, getting the correction for the artist and title]

// eg: for artist := Bucovina and track := Mestecanis, the corrected values are 
//ArtistTitle := Bucovina;
// TrackTitle := Mestecăniș;

//Now here is the tricky part:

webquery := UTF8Encode('http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key=' + apikey + '&artist=' + unescape(ArtistTitle) + '&track=' + unescape(TrackTitle)); 
//the unescape function replaces spaces (' ') with '+' to comply with the last.fm requests

[some more processing]

end;

見た目のwebqueryTMemoはちょうどいいです:

http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key=e5565002840xxxxxxxxxxxxxx23b98ad&artist=Bucovina&track=Mestecăniș

それでも、 (プロパティをに設定して)GETを使用してWebクエリにリクエストを送信しようとすると、ANSIリクエストURLを使用してデータを処理しているWiresharkが表示されます。TIdHTTPContentEncoding'UTF-8'TIdHTTPGET

/2.0/?method=track.getInfo&api_key=e5565002840xxxxxxxxxxxxxx23b98ad&artist=Bucovina&track=Mestec?ni?

GETリクエストとレスポンスの完全なヘッダーは次のとおりです。

GET /2.0/?method=track.getInfo&api_key=e5565002840xxxxxxxxxxxxxx23b98ad&artist=Bucovina&track=Mestec?ni? HTTP/1.1
Content-Encoding: UTF-8
Host: ws.audioscrobbler.com
Accept: text/html, */*
Accept-Encoding: identity
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.23) Gecko/20110920 Firefox/3.6.23 SearchToolbar/1.22011-10-16 20:20:07

HTTP/1.0 400 Bad Request
Date: Tue, 09 Oct 2012 20:46:31 GMT
Server: Apache/2.2.22 (Unix)
X-Web-Node: www204
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Max-Age: 86400
Cache-Control: max-age=10
Expires: Tue, 09 Oct 2012 20:46:42 GMT
Content-Length: 114
Connection: close
Content-Type: text/xml; charset=utf-8;

<?xml version="1.0" encoding="utf-8"?>
<lfm status="failed">
<error code="6">
    Track not found
</error>
</lfm>

私を困惑させる質問は、TIdHTTPコンポーネントのプロパティの設定に関連する何かを監督しているのかということです。アプリケーションで作成している適切な形式のURLが間違った形式でサーバーに送信されないようにするにはどうすればよいですか?

4

2 に答える 2

2

関数からXML応答を取得するには、次のtrack.getCorrectionようなものを使用できます。

uses
  IdHTTP, IdURI;

function GetMusicDataXML(const AArtist, ATrack: string): string;
var
  URL: string;
  IdHTTP: TIdHTTP;
const
  APIKey = '1a3d8080e427f4dxxxxxxxxxxxxxxxxx';
begin
  Result := '';
  IdHTTP := TIdHTTP.Create;
  try
    URL := TIdURI.URLEncode('http://ws.audioscrobbler.com/2.0/?method=track.getcorrection&api_key=' + APIKey + '&artist=' + AArtist + '&track=' + ATrack);
    Result := IdHTTP.Get(URL);
  finally
    IdHTTP.Free;
  end;
end;
于 2012-10-09T22:41:55.530 に答える
2
var
  ...
  webquery    : WideString; 
...
WebQuery := UTF8Encode('http://ws.audioscrobbler.com/2.0/?method=track.getcorrection&api_key=' + apikey + '&artist=' + artist + '&track=' + track); 

これはあなたが思っていることをしません。XE2ではUTF8Encode()、UTF-8でエンコードされたを返します。RawByteStringこれをに割り当てますWideString。RTLは、UTF-8データをデコードしてUTF-16文字列に戻します。その文字列をに渡すとTIdHTTP.Get()、実際のHTTPリクエストがフォーマットされたときにASCIIに変換され、ASCII以外の文字は失われます。

@TLamaが言ったように、URLをTIdURIに渡す前にを使用してURLをエンコードする必要がありますTIdHTTPTIdURIUnicode文字をUTF-8としてエンコードし(デフォルトでは、必要に応じてエンコードを指定できます)、結果のデータをTIdHTTP失われないASCII互換形式でエンコードします。

于 2012-10-09T23:29:20.643 に答える