delphi - 博主通过 idHTTP : error 400 bad request

标签 delphi api delphi-7 indy blogger

我尝试使用 idHTTP 组件向 Blogger 发帖,但是收到“HTTP/1.0 400 Bad Request”错误。

procedure TForm1.Button1Click(Sender: TObject);
var request,response,req  : TStringlist;
    auth,blogid : string;
begin
blogid    := '00000000000000000000000';
request   := TStringList.Create;
response  := TStringList.Create;
req       := TStringList.Create;

IdHTTP1.Request.Connection := 'Keep-Alive';
IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded';
IdSSLIOHandlerSocket1.SSLOptions.Method := sslvSSLv23;

request.Clear();
request.Values['accountType'] := 'GOOGLE';
request.Values['Email']       := 'xxx@gmail.com';
request.Values['Passwd']      := 'yyy';
request.Values['service']     := 'blogger';

response.Text :=IdHTTP1.Post('https://www.google.com/accounts/ClientLogin',request);
auth := response.Values['Auth'];  //working perfectly

req.Clear();
req.Text := Memo2.Lines.Text;  //put entry into request var

IdHTTP1.Request.CustomHeaders.Clear();
IdHTTP1.Request.CustomHeaders.Add('GData-Version: 2');
//IdHTTP1.Request.CustomHeaders.Add('Authorization: GoogleLogin auth='+auth);
IdHTTP1.Request.CustomHeaders.Values['Authorization'] := 'GoogleLogin auth = '+auth;
IdHTTP1.Request.ContentType := 'application/atom+xml';

memo1.Text := IdHTTP1.Post('https://www.blogger.com/feeds/' + blogid + '/posts/default', req); // I got "HTTP/1.0 400 Bad Request" error right here.

end;

发生错误

memo1.Text := IdHTTP1.Post('https://www.blogger.com/feeds/' + blogid + '/posts/default', req);

有人知道我做错了什么吗?

非常感谢任何帮助。谢谢!

最佳答案

您正在使用 TStringList 发布 XML 数据。即使您没有将 Request.ContentType 设置为 application/x-www-form-urlencoded,发布 TStrings 对象将始终对数据就像你做的那样。这可能就是服务器提示的原因,因为它不会正确接收您想要的 XML。要发布 XML 数据,您需要使用 TStream 来代替,例如:

procedure TForm1.Button1Click(Sender: TObject);
var
  request, response : TStringlist;
  req : TStream;
  auth, blogid : String;
begin
  blogid := '00000000000000000000000';

  IdHTTP1.Request.Connection := 'Keep-Alive';
  IdSSLIOHandlerSocket1.SSLOptions.Method := sslvSSLv23;

  request := TStringList.Create;
  try
    response := TStringList.Create;
    try
      request.Values['accountType'] := 'GOOGLE';
      request.Values['Email']       := 'xxx@gmail.com';
      request.Values['Passwd']      := 'yyy';
      request.Values['service']     := 'blogger';

      IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded';
      response.Text := IdHTTP1.Post('https://www.google.com/accounts/ClientLogin', request);

      auth := response.Values['Auth'];
    finally
      response.Free;
    end;
  finally
    request.Free;
  end;

  req := TMemoryStream.Create;
  try
    Memo2.Lines.SaveToStream(req);
    req.Position := 0;

    IdHTTP1.Request.CustomHeaders.Clear;
    IdHTTP1.Request.CustomHeaders.Values['GData-Version'] := '2';
    IdHTTP1.Request.CustomHeaders.Values['Authorization'] := 'GoogleLogin auth = ' + auth;
    IdHTTP1.Request.ContentType := 'application/atom+xml';

    Memo1.Text := IdHTTP1.Post('https://www.blogger.com/feeds/' + blogid + '/posts/default', req);
  finally
    req.Free;
  end;
end;

关于delphi - 博主通过 idHTTP : error 400 bad request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9013443/

相关文章:

Delphi 7 - 将监视添加到使用 'with' 命令引用的变量

delphi - 如何在Windows XP/Windows 2003上部署Delphi Soap Client(.EXE)?

arrays - 更改数组Delphi中的特殊字符

javascript - 收到回发后刷新网页

api - 使用通用搜索 api 实时监控 Twitter 主题标签?

delphi - 如何使编辑控件不接受 0 作为第一位数字?

sql-server - TADOQuery : Passing null as parameter and getting the resultset

delphi - 如何设置 TDBGrid 排序中的字符顺序?

api - 谷歌词典 API 不起作用——还有其他解决方案吗?

delphi - 向 TOpenPictureDialog 添加按钮