首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过idHTTP的博客:错误400错误请求

通过idHTTP的博客:错误400错误请求
EN

Stack Overflow用户
提问于 2012-01-26 10:54:43
回答 1查看 6.4K关注 0票数 3

我正在尝试使用idHTTP组件发布到Blogger,但是,我收到了“HTTP/1.0400 Bad Request”错误。

代码语言:javascript
复制
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;

gor错误打开

代码语言:javascript
复制
memo1.Text := IdHTTP1.Post('https://www.blogger.com/feeds/' + blogid + '/posts/default', req);

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

任何帮助都是非常感谢的。谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-26 11:23:30

您正在使用TStringList发布XML数据。即使您没有将Request.ContentType设置为application/x-www-form-urlencoded,发布一个TStrings对象也会始终对数据进行编码,就像您对数据进行编码一样。这可能是服务器抱怨的原因,因为它不会正确地接收您想要的XML。要发布XML数据,您需要使用TStream,例如:

代码语言:javascript
复制
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;
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9013443

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档