HTTP Post - Funktion? [gelößt, Code im Thread]

Übersicht BlitzBasic Allgemein

Neue Antwort erstellen

hazumu-kun

Betreff: HTTP Post - Funktion? [gelößt, Code im Thread]

BeitragDi, Sep 01, 2009 23:46
Antworten mit Zitat
Benutzer-Profile anzeigen
Hat jemand von euch eine BB Funktion mit der ich per http Post (multipart/form-data) eine Datei hochgeladen bekomme, per php script und BB.

Hab schon was versucht aber anscheinend nicht rihctig verstanden was zutun ist (Error 400: Bad Request).

Ich suche nach einer einfachen möglichkeit über http Post beliebige Dateien hochzuladen, mehr nicht.

mfg, Viken.
Warum kann es keine omnipotente Macht geben?
Weil diese omnipotente Macht in der Lage sein müsste, einen so schweren Stein zu schaffen, dass sie ihn nicht heben kann
-> nicht omnipotent
  • Zuletzt bearbeitet von hazumu-kun am Do, Sep 03, 2009 19:02, insgesamt einmal bearbeitet

Nicdel

BeitragMi, Sep 02, 2009 8:23
Antworten mit Zitat
Benutzer-Profile anzeigen
Zeig mal wie du es versucht hast.
Desktop: Intel Pentium 4 2650 Mhz, 2 GB RAM, ATI Radeon HD 3850 512 MB, Windows XP
Notebook: Intel Core i7 720 QM 1.6 Ghz, 4 GB DDR3 RAM, nVidia 230M GT, Windows 7

hazumu-kun

BeitragMi, Sep 02, 2009 15:20
Antworten mit Zitat
Benutzer-Profile anzeigen
BlitzBasic: [AUSKLAPPEN]

Global str_len

Const remotehost$= "vikenemesh.bplaced.net"
Const remotescript$="/test/uploader/uploader.php"
Const useragent$= "File Uploader"

Global br$= Chr$(10)+Chr$(13)
Global dq$= Chr$(34)

Global boundary$

Global header$

header$= "POST "+remotescript$+" HTTP/1.1"+br$
header$= header$+ "Host: "+remotehost$+br$
header$= header$+ "User-Agent: "+useragent$+br$
header$= header$+ "Keep-Alive: 300"+br$
header$= header$+ "Connection: keep-alive"

Global file_data$
Global file_stream
Global file_name$
Global file$= RequestFile$ ("Wähle die hochzuladende Datei aus.")
If Not FileType (file$) Then Print "Dateifehler!":Input():End

file_name$= strman_filter_filename$ (file$)
file_stream= ReadFile (file$)

While ReadAvail(file_stream)
file_data$= file_data$+ Chr$(ReadByte(file_stream))
Wend

; Boundary(42 bytes): ---------------------------313223033317673
.calcbound
boundary$= "--"
For i= 1 To 40
boundary$= boundary$+ Chr$(Rand(48,57))
Next
If Instr (file_data$,boundary$) Then
Goto calcbound
EndIf

; Content-Type: multipart/form-data; boundary=---------------------------313223033317673
header$= header$+ "Content-Type: multipart/form-data; boundary="+boundary$

Global net_stream

net_stream= OpenTCPStream (remotehost$,80)
WriteLine net_stream,header$
WriteLine net_stream,""
WriteLine net_stream,boundary$
WriteLine net_stream,"Content-Disposition: form-data; name="+dq$+"uploadedfile"+dq$+"; filename="+dq$+file_name$+dq$
WriteLine net_stream,""
WriteLine net_stream,file_data$
WriteLine net_stream,boundary$+"--"
WriteLine net_stream,""

While Not ReadAvail (net_stream)
Delay 50
Wend

While ReadAvail (net_stream)
Print ReadLine$(net_stream)
Wend
Input()
End

; ## String manipulator functions
Function strman_str_len (str_in$)
str_len= Len (str_in$)
End Function

Function strman_turn$ (str_in$)
strman_str_len (str_in$)
Local i
Local str_out$

For i= 1 To str_len
str_out$= Mid$ (str_in$, i, 1)+ str_out$
Next

Return str_out$
End Function

Function strman_filter_filename$ (str_in$)
Local str_pos
Local str_out$

str_in$= strman_turn$ (str_in$)

str_pos= Instr (str_in$, "\")
str_out= strman_turn$ (Left$ (str_in$, (str_pos- 1)))

Return str_out$
End Function
;****
Warum kann es keine omnipotente Macht geben?
Weil diese omnipotente Macht in der Lage sein müsste, einen so schweren Stein zu schaffen, dass sie ihn nicht heben kann
-> nicht omnipotent

BtbN

BeitragMi, Sep 02, 2009 17:11
Antworten mit Zitat
Benutzer-Profile anzeigen
Das erste was mir auffällt: ein CRLF ist \r\n, nicht \n\r, wie du es machst.
Ausserdem: Die -- müssen vor das boundary, nicht dahinter.

hazumu-kun

BeitragMi, Sep 02, 2009 17:48
Antworten mit Zitat
Benutzer-Profile anzeigen
Hmnkay, mal testen.

EDIT:
Das boundary war okay.
Nach dem vertauschen bei meiner br$ Konstante kam immerhin schon ein 200 Statuscode.
Aber mein php script sagt es gäbe ein Problem.

Mit Firefox und html form getstet klappt das hochladen aber komplikationsfrei.
Warum kann es keine omnipotente Macht geben?
Weil diese omnipotente Macht in der Lage sein müsste, einen so schweren Stein zu schaffen, dass sie ihn nicht heben kann
-> nicht omnipotent

hazumu-kun

BeitragMi, Sep 02, 2009 20:38
Antworten mit Zitat
Benutzer-Profile anzeigen
Sry für den Doppelpost aber hab neue Ergebnisse.
Hab von Starwar den hinweis bekommen das man es mal mit banks und read/writebytes versuchen sollte.
folgendes Ergebnis, ohne Erfolg.
200er Statuscode vom Server, Fehler vom script(There was an error uploading the file)
BlitzBasic: [AUSKLAPPEN]

Global str_len

Const remotehost$= "vikenemesh.bplaced.net"
Const remotescript$="/test/uploader/uploader.php"
Const useragent$= "File Uploader"

Global br$= Chr$(13)+Chr$(10)
Global dq$= Chr$(34)

Global boundary$

Global header$

header$= "POST "+remotescript$+" HTTP/1.1"+br$
header$= header$+ "Host: "+remotehost$+br$
header$= header$+ "User-Agent: "+useragent$+br$
header$= header$+ "Keep-Alive: 300"+br$
header$= header$+ "Connection: keep-alive"

Global file_data= CreateBank ()
Global file_text$
Global file_size
Global file_stream
Global file_name$
Global file$= RequestFile$ ("Wähle die hochzuladende Datei aus.")
If Not FileType (file$) Then Print "Dateifehler!":Input():End

file_size= FileSize (file$)
file_name$= strman_filter_filename$ (file$)
file_stream= ReadFile (file$)

ResizeBank file_data,file_size
ReadBytes (file_data,file_stream,0,file_size)

For i= 0 To file_size-1
file_text$= file_text$+ Chr$(PeekByte(file_data,i))
Next

; Boundary(42 bytes): z.b. ---------------------------313223033317673
.calcbound
boundary$= "--"
For i= 1 To 40
boundary$= boundary$+ Chr$(Rand(48,57))
Next
If Instr (file_text$,boundary$) Then
Goto calcbound
EndIf

; Content-Type: multipart/form-data; boundary=---------------------------313223033317673
header$= header$+ "Content-Type: multipart/form-data; boundary="+boundary$

Global net_stream

net_stream= OpenTCPStream (remotehost$,80)
WriteLine net_stream,header$
WriteLine net_stream,""
WriteLine net_stream,boundary$
WriteLine net_stream,"Content-Disposition: form-data; name="+dq$+"uploadedfile"+dq$+"; filename="+dq$+file_name$+dq$
WriteLine net_stream,""
WriteBytes file_data,net_stream,0,file_size
WriteByte net_stream,13
WriteByte net_stream,10
WriteLine net_stream,boundary$+"--"
WriteLine net_stream,""

While Not ReadAvail (net_stream)
Delay 50
Wend

While ReadAvail (net_stream)
Print ReadLine$(net_stream)
Wend
Input()
End

; ## String manipulator functions
Function strman_str_len (str_in$)
str_len= Len (str_in$)
End Function

Function strman_turn$ (str_in$)
strman_str_len (str_in$)
Local i
Local str_out$

For i= 1 To str_len
str_out$= Mid$ (str_in$, i, 1)+ str_out$
Next

Return str_out$
End Function

Function strman_filter_filename$ (str_in$)
Local str_pos
Local str_out$

str_in$= strman_turn$ (str_in$)

str_pos= Instr (str_in$, "\")
str_out= strman_turn$ (Left$ (str_in$, (str_pos- 1)))

Return str_out$
End Function
;****


Acja: von Interesse wäre das remote script allerdings noch:
Code: [AUSKLAPPEN]

<?php
// Where the file is going to be placed
$target_path = "uploads/";

/* Add the original filename to our target path. 
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}
?>
Warum kann es keine omnipotente Macht geben?
Weil diese omnipotente Macht in der Lage sein müsste, einen so schweren Stein zu schaffen, dass sie ihn nicht heben kann
-> nicht omnipotent

hazumu-kun

BeitragDo, Sep 03, 2009 14:52
Antworten mit Zitat
Benutzer-Profile anzeigen
Also, es ist dringend, ich kann an meinem Projekt nicht weiter arbeiten wenn ich kein HTTP Post Proof of Concept hinbekomme.

Wo ist der fehler bei mir?

Mit nem HTML Formular klappt alles reibungslos
Warum kann es keine omnipotente Macht geben?
Weil diese omnipotente Macht in der Lage sein müsste, einen so schweren Stein zu schaffen, dass sie ihn nicht heben kann
-> nicht omnipotent

Holzchopf

Meisterpacker

BeitragDo, Sep 03, 2009 15:17
Antworten mit Zitat
Benutzer-Profile anzeigen
Die SuFu hat mir folgendes ausgespuckt:
https://www.blitzforum.de/foru...327#327327
https://www.blitzforum.de/foru...384#327384
(Beides Posts ausm selben Thread)

Du machst was mit den Boundaries falsch, wenn dein Boundary "Foobar" ist, dann musst du später als Grenze "--Foobar" schicken, am Schluss dann noch "--Foobar--"

BlitzMax: [AUSKLAPPEN]
WriteLine net_stream,boundary$

muss also
BlitzMax: [AUSKLAPPEN]
WriteLine net_stream,"--"+boundary$

sein.
Erledige alles Schritt um Schritt - erledige alles. - Holzchopf
CC BYBinaryBorn - Yogurt ♫ (31.10.2018)
Im Kopf da knackt's und knistert's sturm - 's ist kein Gedanke, nur ein Wurm

BladeRunner

Moderator

BeitragDo, Sep 03, 2009 15:28
Antworten mit Zitat
Benutzer-Profile anzeigen
Noch ein Double/ Triple und ich mach dicht.
Zu Diensten, Bürger.
Intel T2300, 2.5GB DDR 533, Mobility Radeon X1600 Win XP Home SP3
Intel T8400, 4GB DDR3, Nvidia GF9700M GTS Win 7/64
B3D BMax MaxGUI

Stolzer Gewinner des BAC#48, #52 & #92

hazumu-kun

BeitragDo, Sep 03, 2009 18:28
Antworten mit Zitat
Benutzer-Profile anzeigen
Neue Ergebnisse, neuer code.

Folgender Code lädt erfolgreich bei dem oben gennantem script eine beliebige Datei hoch.
Bei der Konsole nicht am 501 Error stören, klappt trotzdem.
BlitzBasic: [AUSKLAPPEN]

; ## HTTP POST Uploader (POC)
; # Author: Viken Emesh
;****

; ## Includes
Include "strman.lib.bb"
;****


; ## Variables
; # File
Global file_path$
Global file_name$
Global file_stream
Global file_size

; # Networking - Connection
Global net_host$ = "vikenemesh.bplaced.net"
Global net_host_res$= "/test/uploader/uploader.php"
Global net_stream = OpenTCPStream (net_host$, 80)
Global net_line$

; # Networking - HTTP
Global http_header$
Global http_useragent$ = "BlitzBasic HTTP Uploader(Windows)"
Global http_boundary$ = "---------------------------"+MilliSecs()
Global http_boundary_len = Len(http_boundary$)
Global http_content$
Global http_content_binary = CreateBank()
Global http_content_len

; # Misc
Global br$ = Chr$(13)+Chr$(10)
Global dq$ = Chr$(34)

Global timer= CreateTimer(10)
;****

; ## Making the use of 'Waittimer' possible
win= CreateWindow ("",-100,-100,0,0,0,16)
;***

; ## Creating header
http_header$= "POST "+net_host_res$+" HTTP/1.1" +br$
http_header$= http_header$ +"Host: "+net_host$ +br$
http_header$= http_header$ +"User-Agent: "+http_useragent$ +br$
http_header$= http_header$ +"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" +br$
http_header$= http_header$ +"Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3" +br$
http_header$= http_header$ +"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" +br$
http_header$= http_header$ +"Keep-Alive: 300" +br$
http_header$= http_header$ +"Connection: keep-alive" +br$
http_header$= http_header$ +"Content-Type: multipart/form-Data; boundary="+http_boundary$ +br$
http_header$= http_header$ +"Content-Length: "
;Still not complete, must be completed after creating the content data.
;****

; ## Reading file to bank
file_path$= RequestFile$ ("Select file to upload.")
file_name$= strman_filter_filename$ (file_path$)
file_size= FileSize (file_path$)
file_stream= ReadFile (file_path$)

ResizeBank http_content_binary,file_size

ReadBytes (http_content_binary,file_stream,0,file_size)
CloseFile file_stream
;****

; ## Generating content
http_content$= "Content-Disposition: form-data; name="+dq$+"uploadedfile"+dq$+"; filename="+dq$+file_name$+dq$ +br$
http_content$= http_content$+ "Content-Type: application/octet-stream"+ br$

http_content_len= file_size+ 2*(http_boundary_len+2)+2 +4 + Len(http_content$)

http_header$= http_header$ + http_content_len + br$
;****

; ## Sending the whole crap
Print "Now uploading..."

DebugLog http_header$
WriteLine net_stream, http_header$

DebugLog "--"+http_boundary$
WriteLine net_stream, "--"+http_boundary$

DebugLog http_content$
WriteLine net_stream, http_content$

;DebugLog br$
;WriteLine net_stream, br$

DebugLog "[binary data]"
WriteBytes (http_content_binary,net_stream,0,file_size)

DebugLog "--"+http_boundary$+"--"
WriteLine net_stream, br$+"--"+http_boundary$+"--"+br$

Print "Done!"
;****

; ## Waiting for response
While Not ReadAvail (net_stream)
Print "Waiting for response..."
WaitTimer (timer)
Wend

While Not Eof (net_stream)
net_line$= ReadLine$ (net_stream)
Print net_line$
DebugLog net_line$
Wend
Print ""
Input ("~END~")

CloseTCPStream net_stream
Stop
End



Achja, und die strman.lib.bb:
BlitzBasic: [AUSKLAPPEN]

;*****************************
;
; STRing MANipulator function LIBrary
;
;-----------------------------
; Author: Viken Emesh
; E-Mail: kevin66@hotmail.de
; Homepage: http://vikenemesh.bplaced.net
; ICQ#: 294-793-975
;=============================
;
; Provides several functions for string manipulation.
;
;*****************************

; ## Globals
Global str_len
;****

; ## Help functions
; -> Saves the length of a string to a Global
; variable to increase memory performance
; (one variable that is permanent instead
; of a variable that is newly generated on
; each function call.).
Function strman_str_len (str_in$)
str_len= Len (str_in$)
End Function
;****

; ## Functions
; -> Returns the given string, but
; in reverse character order.
Function strman_turn$ (str_in$)
strman_str_len (str_in$)
Local i
Local str_out$

For i= 1 To str_len
str_out$= Mid$ (str_in$, i, 1)+ str_out$
Next

Return str_out$
End Function

; -> Returns the filename + extension
; from a given filepath.
Function strman_filter_filename$ (str_in$)
Local str_pos
Local str_out$

str_in$= strman_turn$ (str_in$)

str_pos= Instr (str_in$, "\")
str_out= strman_turn$ (Left$ (str_in$, (str_pos- 1)))

Return str_out$
End Function

; -> Returns the extension of a
; given filename.
Function strman_filter_fileext$ (str_in$)
Local str_pos
Local str_out$

str_in$= strman_turn$ (str_in$)

str_pos= Instr (str_in$, ".")
str_out= strman_turn$ (Left$ (str_in$, str_pos))

Return str_out$
End Function

; -> Returns the path of a full filepath.
Function strman_filter_path$ (str_in$)
Local str_pos
Local str_out$

str_in$= strman_turn$ (str_in$)

str_pos= Instr (str_in$, "\")
str_out$= strman_turn$ (Mid$ (str_in$, str_pos, -1))

Return str_out$
End Function

; -> Enlarges the given string to a given
; length by adding 'leading nils'.
; Parameters
; str_stretch: defines the desired length
Function strman_leadnils$ (str_in$, str_stretch)
strman_str_len (str_in$)
Local str_out$

str_out$= String$( "0", (str_stretch- str_len))+ str_in$

Return str_out$
End Function
;****
Warum kann es keine omnipotente Macht geben?
Weil diese omnipotente Macht in der Lage sein müsste, einen so schweren Stein zu schaffen, dass sie ihn nicht heben kann
-> nicht omnipotent

Neue Antwort erstellen


Übersicht BlitzBasic Allgemein

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group