2012-01-26 177 views
3

我試圖使用idHTTP組件發佈到Blogger,但是,我收到「HTTP/1.0 400錯誤請求」錯誤。在blogger via idHTTP:錯誤400錯誤請求

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

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']  := '[email protected]'; 
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錯誤有誰知道我做錯了嗎?

任何幫助,非常感謝。謝謝!

回答

4

您正在使用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']  := '[email protected]'; 
     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; 
+0

謝謝,但嘗試您的解決方案後,我得到了「HTTP/1.0 401未經授權 –

+0

對不起只需要在這裏刪除空格.. IdHTTP1。 Request.CustomHeaders.Values ['Authorization']:='GoogleLogin auth ='+ auth; –

+0

如果它正在工作,那麼您會接受答案嗎? –