Autor Beitrag
haderle
Hält's aus hier
Beiträge: 10



BeitragVerfasst: Mi 13.04.05 19:18 
Sorry, ich habe jetzt schon hier und in anderen Foren die Lösungsvorschläge versucht, aber bis jetzt leider ohne erfolg.
Ich habe die Suche schon so gut es geht ausgenutzt aber immer das gleiche :(

Ich möchte mit einer Delphi Anwendung ein Paar Daten an ein PHP Script übergeben.
Dieses Speichert dann die Daten als Textdatei auf dem Server. Nur leider klappt das nicht.


Ich habe folgenden Code:

unit1.pas
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls, OleCtrls, SHDocVw, IdMultipartFormData, IdHttp;

type
  TForm1 = class(TForm)
    IdHTTP1: TIdHTTP;
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Parameter1: TStaticText;
    Parameter2: TStaticText;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function HTTPEncode(const AStr: string): string;
const
NoConversion = ['A'..'Z', 'a'..'z', '*', '@', '.', '_', '-'];
var
Sp, Rp: PChar;
begin
SetLength(Result, Length(AStr) * 3);
Sp := PChar(AStr);
Rp := PChar(Result);
while Sp^ <> #0 do
begin
if Sp^ in NoConversion then
Rp^ := Sp^
else if Sp^ = ' ' then
Rp^ := '+'
else
begin
FormatBuf(Rp^, 3, '%%%.2x', 6, [Ord(Sp^)]);
Inc(Rp, 2);
end;
Inc(Rp);
Inc(Sp);
end;
SetLength(Result, Rp - PChar(Result));
end;

procedure TForm1.Button1Click(Sender: TObject);
Var
Werte : TStringStream;
Parameter1Wert : String;
Parameter2Wert : String;
begin
Werte := TStringStream.create('');
IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded';

Parameter1Wert := Edit1.Text;
Parameter2Wert := Edit2.Text;

Werte.WriteString('P1=' + HTTPEncode(Parameter1Wert) + '&');
Werte.WriteString('P2=' + HTTPEncode(Parameter2Wert));
try
Edit1.Text := IdHttp1.Post('http://meine.homepage.tl/Test/script.php', Werte);
except
on E: Exception do
showmessage('Fehler: ' + E.Message);
end;
end;

end.


unit1.dfm
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
object Form1: TForm1
  Left = 0
  Top = 0
  Width = 219
  Height = 146
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 88
    Top = 72
    Width = 75
    Height = 25
    Caption = 'Senden'
    TabOrder = 0
    OnClick = Button1Click
  end
  object Edit1: TEdit
    Left = 72
    Top = 16
    Width = 121
    Height = 21
    TabOrder = 1
  end
  object Edit2: TEdit
    Left = 72
    Top = 40
    Width = 121
    Height = 21
    TabOrder = 2
  end
  object Parameter1: TStaticText
    Left = 8
    Top = 16
    Width = 60
    Height = 17
    Caption = 'Parameter1'
    TabOrder = 3
  end
  object Parameter2: TStaticText
    Left = 8
    Top = 40
    Width = 60
    Height = 17
    Caption = 'Parameter2'
    TabOrder = 4
  end
end


Bildschirmfotos der Fehlermeldung habe ich als Anhang hier beigefügt.
Irgendwie finde ich den Fehler nicht. Könnt Ihr mir da vielleicht weiterhelfen?
Bin über jede Hilfe dankbar.

Schonmal Danke und Gruß
Micha
Einloggen, um Attachments anzusehen!
Hansi@OMG
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 304

Vista
Delphi 2006 Prof., Lazarus
BeitragVerfasst: Mi 13.04.05 20:17 
Probiers mal nicht mit einem TStringStream, sondern mit einer TStringList!

_________________
Who doesn't know the Micrsoft developer "Mahatma Fatal Error"?
haderle Threadstarter
Hält's aus hier
Beiträge: 10



BeitragVerfasst: Mi 13.04.05 20:30 
danke für die antwort, aber hab es jetzt mit:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
.....
Var
Werte : TStringList;
Parameter1Wert : String;
Parameter2Wert : String;
begin
Werte := TStringList.create;
IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded';

Parameter1Wert := Edit1.Text;
Parameter2Wert := Edit2.Text;

Werte.Add('P1=' + HTTPEncode(Parameter1Wert) + '&');
Werte.Add('P2=' + HTTPEncode(Parameter2Wert));
.....


probiert... leider gleiche fehler :(


gruß Micha
Hansi@OMG
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 304

Vista
Delphi 2006 Prof., Lazarus
BeitragVerfasst: Mi 13.04.05 20:52 
Du hast am Ende vergessen, die Stringliste wieder freizugeben.
Das löst normalerweise eine Accessviolation aus.
Füg mal Werte.free am Ende ein.

_________________
Who doesn't know the Micrsoft developer "Mahatma Fatal Error"?
haderle Threadstarter
Hält's aus hier
Beiträge: 10



BeitragVerfasst: Mi 13.04.05 21:05 
mhh... ja, aber dennoch "Zugriffsverletzung bei Adresse 0048659C in Modul 'Project1.exe'. Lesen von Adresse 00000100" :?
Hansi@OMG
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 304

Vista
Delphi 2006 Prof., Lazarus
BeitragVerfasst: Do 14.04.05 06:10 
Ich code im Laufe des Tages mal ein Testbeispiel, dann post ich hier das Ergebnis.

_________________
Who doesn't know the Micrsoft developer "Mahatma Fatal Error"?
patrick
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1481

WIN2k, WIN XP
D6 Personal, D2005 PE
BeitragVerfasst: Do 14.04.05 08:34 
klick mal zur laufzeit in deine delphi-menüleiste unter "suchen"->"laufzeitfehler suchen" und gib dort die adresse "0048659C" ein.
mit etwas glück ladest du in der bösen programmzeile.
ansonsten, einfach mal ein paar breakpoints setzen und stück für stück durch den quelltext springen

_________________
Patrick
im zweifelsfall immer das richtige tun!!!
haderle Threadstarter
Hält's aus hier
Beiträge: 10



BeitragVerfasst: Do 14.04.05 19:02 
Hi :) ,
habe es jetzt mal mit den Breakpoints gemacht, da ich "Laufzeitfehler suchen" nicht habe bzw. finde, und es sind zwei Zeilen an dem Fehler schuld:

1. Zeile
ausblenden Delphi-Quelltext
1:
IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded';					


2. Zeile
ausblenden Delphi-Quelltext
1:
Edit1.Text := IdHttp1.Post('http://meine.homepage.tl/Test/script.php', Werte);					


Also vermute ich mal das es am "IdHTTP1" liegt...
Jetzt nur noch die Frage... Wie löse ich das Problem? :?

Sorry das ich so viele dumme Fragen stelle.
Ist mir richtig peinlich bei den ganzen Profis hier. :oops:
Hansi@OMG
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 304

Vista
Delphi 2006 Prof., Lazarus
BeitragVerfasst: Do 14.04.05 19:35 
Ich hab dir jetzt mal ein ganz einfaches Beispiel geschrieben (Siehe Anhang).
Einloggen, um Attachments anzusehen!
_________________
Who doesn't know the Micrsoft developer "Mahatma Fatal Error"?
haderle Threadstarter
Hält's aus hier
Beiträge: 10



BeitragVerfasst: Do 14.04.05 19:59 
Oh man is mir das jetzt peinlich.... :oops: :oops: :oops:

Danke Euch beiden für die liebe Hilfe. Es hätte mir eigentlich schon beim Debuggen auffallen müssen,
das es daran liegt, das irgendwie das "TIdHTTP" bei mir verschwunden ist bzw. gefehlt hat...
Passiert halt wenn man versucht alles nur per Hand zu schreiben. :D

Aber endgültig hat jetzt der Source von Hansi@OMG mir die Augen geöffnet.
Als ich dein Beispiel geöffnet habe is mir gleich das "TIdHTTP" ins Auge gesprungen und da war mir alles klar. :oops:

Also großes danke nochmal Euch beiden.

Gruß Micha
Hansi@OMG
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 304

Vista
Delphi 2006 Prof., Lazarus
BeitragVerfasst: Do 14.04.05 20:21 
Aber immer doch, wofür gibt es denn so super Foren wie das Delphi-Forum!!! :D

_________________
Who doesn't know the Micrsoft developer "Mahatma Fatal Error"?
patrick
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1481

WIN2k, WIN XP
D6 Personal, D2005 PE
BeitragVerfasst: Do 14.04.05 21:35 
:shock: :? :| :) :lol: :D :P :rofl: :mrgreen:

_________________
Patrick
im zweifelsfall immer das richtige tun!!!
magggus
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Do 05.05.05 12:14 
Hey war gerade an etwas ähnlichem dran.

allerdings würde ich gerne ein bild übertragen....

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
httpdat :=  TidHTTP.create(nil);
httpdat.Request.ContentType := 'multipart/form-data';

picture := TidMultiPartFormDataStream.create;
picture.AddFile('file', 'd:\bild.jpg', 'image/jpeg');

httpdat.post('http://localhost/upload.php', picture);


im php script sind aber keine daten da.
per html upload funktioniert das (genau mit dem selben empfangenden script) schon

htm form:
ausblenden Quelltext
1:
2:
3:
4:
<form action="upload.php" method="post" enctype="multipart/form-data">
 <input type="file" name="file">
 <input type="submit">
</form>



im php script arbeite ich mit:

ausblenden Quelltext
1:
2:
3:
$file
$file_name
$file_size

im php script ist $file "nil" ...

ehrlich gesagt hab ich kein plan mehr an was es liegen könnte.

Moderiert von user profile iconraziel: Delphi- und Code-Tags hinzugefügt
wulfskin
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1349
Erhaltene Danke: 1

Win XP
D5 Pers (SSL), D2005 Pro, C, C#
BeitragVerfasst: Do 05.05.05 12:55 
Keine Ahnung ob es daran liegt, aber normalerweise greift man in PHP so darauf zu:
ausblenden Quelltext
1:
2:
  $_FILES["datei"]["name"]
  $_FILES["datei"]["size"]
Usw...

Man sollte schon sauberes PHP programmieren und nicht irgend etwas hinklatschen. Soviel zur letzten Diskussion, was ist besser?!

Gruß Hape!

_________________
Manche antworten um ihren Beitragszähler zu erhöhen, andere um zu Helfen.
magggus
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Do 05.05.05 13:04 
hi,

hab nochmal gesucht.
diese sache hab ich korrigiert.

habe jetzt alles nochmal getestet.
mit dem upload per form klappt es immer noch per delphi aber immer noch nicht.

beim debuggen bekomm ich gesagt, dass es eine file grösse hat ... und auch einen typ.
was ich allerdings noch nicht ganz einordnen kann ist:

bei addfile(formfieldname,filename,contenttype)

steht bei content type: image/jpeg
ist hier vielleicht der typ des input tags gemeint zb "file"

hab ich aber auch schon probiert.


sehr merkwürdig alles ... hmm
magggus
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Do 05.05.05 14:18 
hab "meinen" fehler gefunden.

das problem war wohl eine falsche version der eingebauten delphi komponenten.


www.delphipraxis.net...c11441,0,asc,15.html

hier schon mal beschrieben.


jetzt klappts.
war also alles von anfang an ok, wenn auch nicht schön (bzg auf php code)


trotzdem danke!!!!


gruss maggus