以下是我的代码(在一个带有Button1、Memo1和IdHTTP1的表单上,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>';
替换actionurl字符串。
ParseError消息让我相信在发布longurl时可能存在编码问题,但我不知道该改变什么。
我已经重复检查这个问题很长一段时间了,我相信错误就在我的眼前-我现在只是看不到它。
非常感谢您的帮助!
谢谢!