2010-04-23 30 views
6

我想使用TIdHTTP組件從Delphi 2006內部的maps.google.com返回內容。如何使用TIdHTTP獲取Google靜態地圖?

我的代碼如下

procedure TForm1.GetGoogleMap(); 
var 
    t_GetRequest: String; 
    t_Source: TStringList; 
    t_Stream: TMemoryStream; 
begin 
    t_Source := TStringList.Create; 

    try 
    t_Stream := TMemoryStream.Create; 

    try 
     t_GetRequest := 
     'http://maps.google.com/maps/api/staticmap?' + 
     'center=Brooklyn+Bridge,New+York,NY' + 
     '&zoom=14' + 
     '&size=512x512' + 
     '&maptype=roadmap' + 
     '&markers=color:blue|label:S|40.702147,-74.015794' + 
     '&markers=color:green|label:G|40.711614,-74.' + 
     '&markers=color:red|color:red|label:C|40.718217,-73.998284' + 
     '&sensor=false'; 

     IdHTTP1.Post(t_GetRequest, t_Source, t_Stream); 

     t_Stream.SaveToFile('google.html'); 
    finally 
     t_Stream.Free; 
    end; 
    finally 
    t_Source.Free; 
    end; 
end; 

但是我不斷收到響應HTTP/1.0 403禁止。我認爲這意味着我沒有權限發出這個請求,但是如果我將url複製到我的網頁瀏覽器IE 8中,它可以正常工作。有一些我需要的標題信息或其他內容嗎?

回答

0

您是否需要身份驗證的代理服務器?

我沒有再安裝德爾福,但我認爲,Indy組件支持代理服務器驗證,有點像......(未經測試)

IdHTTP1.ProxyParams.ProxyServer := 'http://proxyaddress'; 
idHTTP1.ProxyParams.ProxyPort := 8080; 
idHTTP.ProxyParams.ProxyUserName := 'name'; 
idHTTP.ProxyParams.ProxyPassword := 'pwd'; 
4

你正在做一個POST請求,但您的瀏覽器會正在做一個GET請求;改變你的delphi代碼也做一個GET請求。

谷歌可能被UserAgent攔截;嘗試清除它,或將其更改爲與您的瀏覽器相匹配。

0

就像水珠說,你需要做的get(),而不是帖子(),例如:

procedure TForm1.GetGoogleMap(); 
var 
    t_GetRequest: String; 
    t_Stream: TMemoryStream; 
begin 
    t_Stream := TMemoryStream.Create; 
    try 
    t_GetRequest := 
     'http://maps.google.com/maps/api/staticmap?' + 
     'center=Brooklyn+Bridge,New+York,NY' + 
     '&zoom=14' + 
     '&size=512x512' + 
     '&maptype=roadmap' + 
     '&markers=color:blue|label:S|40.702147,-74.015794' + 
     '&markers=color:green|label:G|40.711614,-74.' + 
     '&markers=color:red|color:red|label:C|40.718217,-73.998284' + 
     '&sensor=false'; 

    IdHTTP1.Get(t_GetRequest, t_Stream); 

    t_Stream.SaveToFile('google.html'); 
    finally 
    t_Stream.Free; 
    end; 
end; 
1

cloudstrif3,通過請求返回的值是一個形象不是html頁面,我只是在我的博客上寫了這篇文章Using Google maps (Static Maps) without TWebBrowser,所以你可以查看源代碼。

檢查此示例。

var 
    StreamData :TMemoryStream; 
    JPEGImage : TJPEGImage; 
    Url  : string; 
begin 
    Url  :='http://maps.google.com/maps/api/staticmap?' + 
    'center=Brooklyn+Bridge,New+York,NY' + 
    '&zoom=14' + 
    '&size=512x512' + 
    '&maptype=roadmap' + 
    '&markers=color:blue|label:S|40.702147,-74.015794' + 
    '&markers=color:green|label:G|40.711614,-74.' + 
    '&markers=color:red|color:red|label:C|40.718217,-73.998284' + 
    '&format=jpg'; //i add this param to return a jpg image , because the default format used is png. 
    '&sensor=false'; 
    StreamData := TMemoryStream.Create; 
    JPEGImage := TJPEGImage.Create; 
    try 
    try 
    idhttp1.Get(Url, StreamData); //Send the request and get the image 
    StreamData.Seek(0,soFromBeginning); 
    JPEGImage.LoadFromStream(StreamData);//load the image in a Stream 
    ImageMap.Picture.Assign(JPEGImage);//Load the image in a Timage component 
    Except On E : Exception Do 
    MessageDlg('Exception: '+E.Message,mtError, [mbOK], 0); 
    End; 
    finally 
    StreamData.free; 
    JPEGImage.Free; 
    end; 
end; 
0
const 
NONE    = $00; //Zero-value number 
INET_USERAGENT = 'Mozilla/4.0, Indy Library (Windows; en-US)'; 
INET_REDIRECT_MAX = 10; 
var 
    StreamData :TMemoryStream; 
    JPEGImage : TJPEGImage; 
    Url,html  : string; 

begin 
    idhttp1.request.userAgent:=INET_USERAGENT;        //Define user agent 
    idhttp1.redirectMaximum:=INET_REDIRECT_MAX;       //Redirect maxumum 
    idhttp1.handleRedirects:=INET_REDIRECT_MAX<>NONE; 
    Url  :=edit1.Text; 
    StreamData := TMemoryStream.Create; 
    JPEGImage := TJPEGImage.Create; 

    try 
    try 
    html:= idhttp1.Get(Url); //Send the request and get the image 

    idhttp1.Get(Url, StreamData); //Send the request and get the image 
    StreamData.Seek(0,soFromBeginning); 
    memo1.Lines.Text:=html; 
    JPEGImage.LoadFromStream(StreamData);//load the image in a Stream 
    ImageMap.Picture.Assign(JPEGImage);//Load the image in a Timage component 

    Except On E : Exception Do 
    MessageDlg('Exception: '+E.Message,mtError, [mbOK], 0); 
    End; 
    finally 
    StreamData.free; 
    JPEGImage.Free; 
    end; 
end; 
0

我重塑編碼從頭:-)

This question同樣的事情輪&解釋爲什麼你會得到403

我加入這行代碼&它工作了我:
idHttp.request.useragent := 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; MAAU)';

是的,我做了注意這個問題是3歲(並且從未授予答案),但是我附加這個來幫助其他人。

相關問題