блоггер через idHTTP: ошибка 400 неверный запрос - PullRequest
3 голосов
/ 26 января 2012

Я пытаюсь опубликовать сообщение в Blogger, используя компонент idHTTP, однако получаю ошибку «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;

gor error на

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

Кто-нибудь знает, что я делаю неправильно?

Любая помощь очень ценится.Спасибо!

1 Ответ

5 голосов
/ 26 января 2012

Вы публикуете свои данные XML, используя TStringList. Даже если вы не устанавливаете 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;
...