我想通過Delphi中的API訪問URL Shortener(http://goo.gl/)。 然而,唯一的結果我得到的是:HTTP/1.0 400錯誤的請求(原因:parseError)德爾福:使用Google URL Shortener與IdHTTP - 400錯誤請求
這裏是我的代碼(窗體上的一個Button1的,Memo1和具有IdHTTP1 。IdSSLIOHandlerSocketOpenSSL1爲IOHandler我從http://indy.fulgan.com/SSL/得到了必要的32位的OpenSSL DLL文件並把它們放在.exe文件的目錄):
procedure TFrmMain.Button1Click(Sender: TObject);
var html, actionurl: String;
makeshort: TStringList;
begin
try
makeshort := TStringList.Create;
actionurl := 'https://www.googleapis.com/urlshortener/v1/url';
makeshort.Add('{"longUrl": "http://slashdot.org/stories"}');
IdHttp1.Request.ContentType := 'application/json';
//IdHTTP1.Request.ContentEncoding := 'UTF-8'; //Using this gives error 415
html := IdHTTP1.Post(actionurl, makeshort);
memo1.lines.add(idHTTP1.response.ResponseText);
except on e: EIdHTTPProtocolException do
begin
memo1.lines.add(idHTTP1.response.ResponseText);
memo1.lines.add(e.ErrorMessage);
end;
end;
memo1.Lines.add(html);
makeshort.Free;
end;
更新:我已經離開了在這個例子中我的API密鑰(通常應該沒有一個試了幾次不錯的選擇),但如果你想用你自己去嘗試,你可以用 'https://www.googleapis.com/urlshortener/v1/url?key=<yourapikey>';
ParseError消息讓我相信longurl的編碼在發佈時可能會出錯,但我不知道要更改什麼。
我一直在模糊這一段時間,現在我確定這個錯誤在我眼前 - 我現在只是沒有看到它。 因此非常感謝任何幫助!
謝謝!
我發現自己畢竟,它的偉大工程的答案,它有沒有關係後,所有的API密鑰。不幸的是,由於我是一個新用戶,並且結果不符合評論框,所以出於某些深不可測的原因,我無法將本人的答案與本網站上的新代碼一起發佈8個小時。我不知道限制是要達到什麼目的。好吧,太糟糕了。 – user1579166 2012-08-06 14:49:28