views:

364

answers:

5

I'm trying to return content from maps.google.com from within Delphi 2006 using the TIdHTTP component.

My code is as follows

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;

However I keep getting the response HTTP/1.0 403 Forbidden. I assume this means that I don't have permission to make this request but if I copy the url into my web browser IE 8, it works fine. Is there some header information that I need or something else?

A: 

Are you behind a Proxy Server that needs authentication?

I don't have Delphi installed anymore but I think that the Indy components support Proxy authentication, something like...(untested)

IdHTTP1.ProxyParams.ProxyServer := 'http://proxyaddress';
idHTTP1.ProxyParams.ProxyPort := 8080;
idHTTP.ProxyParams.ProxyUserName := 'name';
idHTTP.ProxyParams.ProxyPassword := 'pwd';
Tim Jarvis
+3  A: 

you're doing a POST request, however your browser will be doing a GET request; change your delphi code to also do a GET request.

google may be blocking by UserAgent; try clearing it, or changing it to match your browser's.

glob
A: 

Yes changing the UserAgent property to Mozilla/4.0 solved the problem. Both post and get seem to fine. Thank you for help.

Cloud
The better way would have been to accept glob's answer and write this as a comment.
Smasher
Welcome to Stack Overflow. Please note that to accept an answer, you need to be logged in using the same account that you used to post the question. If you use the "contact us" link at the bottom of the page, you can ask an administrator to merge your two accounts into one.
Rob Kennedy
A: 

Like glob said, you need to do a Get() instead of a Post(), for example:

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;  
Remy Lebeau - TeamB
+1  A: 

cloudstrif3, the value returned by the request is a image not a html page, i just wrote this article Using Google maps (Static Maps) without TWebBrowser on my blog, so you can check the source code.

check this sample.

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;
RRUZ