德尔福:使用谷歌URL缩短与IdHTTP - 400错误的请求 [英] Delphi: Using Google URL Shortener with IdHTTP - 400 Bad Request

查看:434
本文介绍了德尔福:使用谷歌URL缩短与IdHTTP - 400错误的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过其API来访问URL缩短( http://goo.gl/ )德尔福以内。
不过,唯一的结果我得到的是: HTTP / 1.0 400错误的请求(原因: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)

下面是我的code(用的 Button1的 Memo1 的和的 IdHTTP1 的有 IdSSLIOHandlerSocketOpenSSL1 作为其IOHandler。我从 http://indy.fulgan.com/必要的32位的DLL的OpenSSL SSL / ,然后把它们放在.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=<yourapikey>';

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.

我一直在模糊​​化在这个相当长的一段时间,我在我眼前,我相信这个错误是 - 我只是没有看到它现在。
任何帮助因此大大AP preciated!

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的字符串列表重载版本( )的方法是用错误的方法。它发送一个应用程序/ x-WWW的形式urlen codeD 格式的请求,这是不恰当的JSON格式的请求。你必须使用 TStream 重载版本的 TIdHTTP.Post()方法instead`的,例如:

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; 

这篇关于德尔福:使用谷歌URL缩短与IdHTTP - 400错误的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆