Aufruf einer Funktion funktionoiert nur über einen Button, nicht aber über Form_load - VB6

  • VB6

Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von haiflosse.

    Aufruf einer Funktion funktionoiert nur über einen Button, nicht aber über Form_load - VB6

    Ich rufe eine Funktion auf, die einen Post-Request eines HTML Formulares durchführen soll.
    Das ganze funktioniert aber nur wenn man dies über einen Button von einem Formular aufruft.
    Wenn ich dies automatisch z.b. über Form_Load starte wird die Seite nicht ausgeführt.

    Hier ein Auszug aus meinem Code:

    Aufruf: gpsdistance("datei.txt")

    Quellcode

    1. Public Function gpsdistance(datei As String) As Double
    2. Dim strFile As String
    3. Dim strHttp As String
    4. Dim DestUrl As URL
    5. Dim intdisplay As Integer
    6. strdatei = Mid(datei, InStrRev(datei, "\", -1) + 1)
    7. strUrl = "http://www.gpsvisualizer.com/convert?output"
    8. strName = "uploaded_file_1"
    9. strname2 = "convert_add_distance"
    10. strMIMEType = "multipart/form-data"
    11. If blnConnected Then Exit Function
    12. ' check that a file was selected
    13. If datei = vbNullString Then
    14. MsgBox "No File Chosen", vbCritical, "ERROR"
    15. Exit Function
    16. End If
    17. ' extract the URL using a helper function
    18. DestUrl = ExtractUrl(strUrl)
    19. If DestUrl.Host = vbNullString Then
    20. MsgBox "Invalid Host", vbCritical, "ERROR"
    21. Exit Function
    22. End If
    23. ' clear the old response
    24. strResponse = ""
    25. ' read the file contents as a string
    26. ' N.B: in HTTP everything is a string, even binary files
    27. strFile = GetFileContents(datei)
    28. ' build the HTTP request
    29. strHttp = BuildFileUploadRequest2(strFile, DestUrl, strName, strdatei, strname2, strMIMEType)
    30. ' assign the protocol host and port
    31. Winsock1.Protocol = sckTCPProtocol
    32. Winsock1.RemoteHost = DestUrl.Host
    33. If DestUrl.Port <> 0 Then
    34. Winsock1.RemotePort = DestUrl.Port
    35. Else
    36. Winsock1.RemotePort = 80
    37. End If
    38. ' make the connection and send the HTTP request
    39. Winsock1.Connect
    40. While Not blnConnected
    41. DoEvents
    42. Wend
    43. strRequest = strHttp
    44. Clipboard.Clear
    45. Clipboard.SetText strHttp
    46. Winsock1.SendData strHttp
    47. While blnConnected
    48. DoEvents
    49. Wend
    50. 'Clipboard.SetText strResponse
    51. 'Debug.Print Len(strResponse)
    52. Dim intview As Integer
    53. Dim pfad As String
    54. intdisplay = InStr(1, strResponse, "display")
    55. If intdisplay > 0 Then
    56. pfad = "www.gpsvisualizer.com" & Mid(strResponse, intdisplay - 1, InStr(intdisplay, strResponse, ">") - intdisplay)
    57. gpsdistance = distance(pfad)
    58. ' gpslink = Mid(pfad, InStrRev(pfad, "/", -1))
    59. 'gpslink = "http://www.gpsvisualizer.com/display" & gpslink & " "
    60. Else
    61. gpsdistance = 0
    62. End If
    63. If blnConnected Then blnConnected = False
    64. End Function


    Quellcode

    1. Private Function BuildFileUploadRequest2(ByRef strData As String, _
    2. ByRef DestUrl As URL, _
    3. ByVal UploadName As String, _
    4. ByVal FileName As String, _
    5. ByVal uploadname2 As String, _
    6. ByVal MimeType As String) As String
    7. Dim strHttp As String ' holds the entire HTTP request
    8. Dim strBoundary As String 'the boundary between each entity
    9. Dim strBody As String ' holds the body of the HTTP request
    10. Dim lngLength As Long ' the length of the HTTP request
    11. ' create a boundary consisting of a random string
    12. strBoundary = RandomAlphaNumString(32)
    13. wert1 = "convert_add_distance"
    14. wert2 = "true"
    15. ' create the body of the http request in the form
    16. '
    17. ' --boundary
    18. ' Content-Disposition: form-data; name="UploadName"; filename="FileName"
    19. ' Content-Type: MimeType
    20. '
    21. ' file data here
    22. '--boundary--
    23. strBody = "--" & strBoundary & vbCrLf
    24. strBody = strBody & "Content-Disposition: form-data; name=""" & wert1 & """" & vbCrLf & vbCrLf & " True "
    25. strBody = strBody & vbCrLf & "--" & strBoundary & vbCrLf
    26. strBody = strBody & "Content-Disposition: form-data; name=""" & UploadName & """; filename=""" & _
    27. FileName & """" & vbCrLf
    28. strBody = strBody & "Content-Type: " & MimeType & vbCrLf
    29. strBody = strBody & vbCrLf & strData
    30. strBody = strBody & vbCrLf & "--" & strBoundary & "--"
    31. 'strBody = strBody & "--" & strBoundary & vbCrLf
    32. lngLength = Len(strBody)
    33. ' construct the HTTP request in the form:
    34. '
    35. ' POST /path/to/reosurce HTTP/1.0
    36. ' Host: host
    37. ' Content-Type: multipart-form-data, boundary=boundary
    38. ' Content-Length: len(strbody)
    39. '
    40. ' HTTP request body
    41. strHttp = "POST " & DestUrl.URI & "?" & DestUrl.Query & " HTTP/1.0" & vbCrLf
    42. strHttp = strHttp & "Host: " & DestUrl.Host & vbCrLf
    43. strHttp = strHttp & "Content-Type: multipart/form-data, boundary=" & strBoundary & vbCrLf
    44. strHttp = strHttp & "Content-Length: " & lngLength & vbCrLf & vbCrLf
    45. strHttp = strHttp & strBody
    46. BuildFileUploadRequest2 = strHttp
    47. End Function


    Beim Aufruf über form_load bleibt er immer im Code:
    While Not blnConnected
    DoEvents
    Wend

    hängen.

    Hoffe es kann mir jemand weiterhelfen.
    @haiflosse Bist Du sicherf, dass Du von VB6 redest?
    Falls Nein, ändere bite den Kopf dieses Deines Threads.
    [Allgemein] VB.NET, VB6, VBA, VBS — Mit welcher Sprache programmiere ich eigentlich?
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    @RodFromGermany

    RodFromGermany schrieb:

    Bist Du sicherf, dass Du von VB6 redest?

    Dachte ich zuerst auch, dann sah ich das Wend. Ich denke das würde unter .Net gar nicht mehr kompilieren oder. Kann mich aber irren.

    Grüße
    If _work = worktype.hard Then Me.Drink(Coffee)
    Seht euch auch meine Tutorialreihe <WPF Lernen/> an oder abonniert meinen YouTube Kanal.

    ## Bitte markiere einen Thread als "Erledigt" wenn deine Frage beantwortet wurde. ##