如何使用TIdHTTP获取Google静态地图?

时间:2010-04-23 04:33:34

标签: delphi google-maps

我正在尝试使用TIdHTTP组件从Delphi 2006中返回maps.google.com中的内容。

我的代码如下

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.012318' +
        '&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 Forbidden的响应。我认为这意味着我没有权限提出此请求但是如果我将网址复制到我的网络浏览器IE 8中,它可以正常工作。是否有我需要的标题信息或其他内容?

6 个答案:

答案 0 :(得分:4)

您正在执行POST请求,但您的浏览器将执行GET请求;更改您的delphi代码以执行GET请求。

Google可能会被UserAgent阻止;尝试清除它,或更改它以匹配您的浏览器。

答案 1 :(得分: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.012318' +  
  '&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;

答案 2 :(得分:0)

您是否需要身份验证的代理服务器?

我没有安装Delphi,但我认为Indy组件支持代理身份验证,类似于......(未经测试)

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

答案 3 :(得分:0)

就像glob说的那样,你需要做一个Get()而不是Post(),例如:

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.012318' +  
      '&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;  

答案 4 :(得分: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;

答案 5 :(得分:0)

我正在重新发明轮子和从头开始编写相同的东西: - )

This question解释了为什么你会得到403。

我添加了这行代码&amp;它对我有用:
idHttp.request.useragent := 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; MAAU)';

是的,我确实知道这个问题已经过了3年(并且没有给出答案),但我将其附加到其他人身上。