2010-04-23 13 views
6

Estoy intentando devolver el contenido de maps.google.com desde Delphi 2006 utilizando el componente TIdHTTP.¿Cómo puedo obtener mapas estáticos de Google con TIdHTTP?

Mi código es el siguiente

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; 

Sin embargo me siguen dando la respuesta HTTP/1.0 403 Forbidden. Supongo que esto significa que no tengo permiso para realizar esta solicitud, pero si copio la URL en mi navegador web IE 8, funciona bien. ¿Hay alguna información de encabezado que necesito o algo más?

Respuesta

0

¿Está detrás de un servidor proxy que necesita autenticación?

mi no me ha instalado Delphi más, pero creo que los componentes Indy admitir la autenticación de proxy, algo así como ... (no probado)

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

que está haciendo una solicitud POST, sin embargo su navegador estar haciendo una solicitud GET; cambie su código delphi para hacer también una solicitud GET.

google puede estar bloqueando por UserAgent; intente borrarlo o cambiarlo para que coincida con el de su navegador.

0

Como pegote Dicho esto, usted tiene que hacer un get() en lugar de un mensaje(), por ejemplo:

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, el valor devuelto por la solicitud es una imagen no una página html, i Acabo de escribir este artículo Using Google maps (Static Maps) without TWebBrowser en mi blog, por lo que puede consultar el código fuente.

ver esta muestra.

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

estoy reinventando la rueda de codificación & lo mismo desde cero :-)

This question explica por qué se obtiene 403.

añadí esta línea de código & que trabajó para yo:
idHttp.request.useragent := 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; MAAU)';

Sí, lo hago Alize que la pregunta es de 3 años (y nunca se otorgó una respuesta), pero añado esto para ayudar a otros.

Cuestiones relacionadas