Delphi:将 Google URL Shortener 与 IdHTTP 结合使用 - 400 错误请求 [英] Delphi: Using Google URL Shortener with IdHTTP - 400 Bad Request
问题描述
我正在尝试通过其 API 访问 URL Shortener ( http://goo.gl/ )在德尔福内部.但是,我得到的唯一结果是:HTTP/1.0 400 Bad Request(原因:parseError)
I'm trying to access the URL Shortener ( http://goo.gl/ ) via its API from within Delphi. However, the only result I get is: HTTP/1.0 400 Bad Request (reason: parseError)
这是我的代码(在带有 Button1、Memo1 和 IdHTTP1 的表单上,其中 IdSSLIOHandlerSocketOpenSSL1 作为它的 IOHandler.我从 http://indy.fulgan.com/SSL/<获得了必要的 32 位 OpenSSL DLL/a> 并将它们放在 .exe 的目录中):
Here is my code (on a form with a Button1, Memo1 and IdHTTP1 that has IdSSLIOHandlerSocketOpenSSL1 as its IOHandler. I got the necessary 32-bit OpenSSL DLLs from http://indy.fulgan.com/SSL/ and put them in the .exe's directory):
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 密钥(如果没有一个通常应该可以很好地尝试几次),但是如果你想用你自己的来尝试它,你可以替换actionurl 字符串与'https://www.googleapis.com/urlshortener/v1/url?key=
Update: I have left off my API key in this example (should usually work well without one for a few tries), but if you want to try it with your own, you can substitute the actionurl string with
'https://www.googleapis.com/urlshortener/v1/url?key=<yourapikey>';
ParseError 消息让我相信 longurl 发布时的编码可能有问题,但我不知道要更改什么.
The ParseError message leads me to believe that there might be something wrong with the encoding of the longurl when it gets posted but I wouldn't know what to change.
我已经模糊了很长一段时间,我确信这个错误就在我眼前 - 我只是现在没有看到它.因此,非常感谢任何帮助!
I've been fuzzing over this for quite a while now and I'm sure the mistake is right before my eyes - I'm just not seeing it right now. Any help is therefore greatly appreciated!
谢谢!
推荐答案
正如您所发现的,TIdHTTP.Post()
方法的 TStrings
重载版本是错误的使用的方法.它发送一个 application/x-www-form-urlencoded
格式的请求,这不适用于 JSON 格式的请求.您必须改用 TIdHTTP.Post()
方法的 TStream
重载版本`,例如:
As you discovered, the TStrings
overloaded version of the TIdHTTP.Post()
method is the wrong method to use. It sends an application/x-www-form-urlencoded
formatted request, which is not appropriate for a JSON formatted request. You have to use the TStream
overloaded version of the TIdHTTP.Post()
method instead`, eg:
procedure TFrmMain.Button1Click(Sender: TObject);
var
html, actionurl: String;
makeshort: TMemoryStream;
begin
try
makeshort := TMemoryStream.Create;
try
actionurl := 'https://www.googleapis.com/urlshortener/v1/url';
WriteStringToStream(makeshort, '{"longUrl": "http://slashdot.org/stories"}', IndyUTF8Encoding);
makeshort.Position := 0;
IdHTTP1.Request.ContentType := 'application/json';
IdHTTP1.Request.Charset := 'utf-8';
html := IdHTTP1.Post(actionurl, makeshort);
finally
makeshort.Free;
end;
Memo1.Lines.Add(IdHTTP1.Response.ResponseText);
Memo1.Lines.Add(html);
except
on e: Exception do
begin
Memo1.Lines.Add(e.Message);
if e is EIdHTTPProtocolException then
Memo1.lines.Add(EIdHTTPProtocolException(e).ErrorMessage);
end;
end;
end;
这篇关于Delphi:将 Google URL Shortener 与 IdHTTP 结合使用 - 400 错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!