Results 1 to 40 of 87

Thread: Winsock: Making HTTP POST/GET Requests

Hybrid View

  1. #1

    Thread Starter
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Winsock: Making HTTP POST/GET Requests

    The problem is caused by the maximum string size in VB6. If it exceeds 32KB it is truncated; I just viewed the Yahoo source coed and it appears that it is about 50KB.

    If you want to be able to use larger strings then you need to use text stream and load the data into either a rich text box or a file.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  2. #2
    New Member
    Join Date
    Jun 2008
    Posts
    5

    Re: Winsock: Making HTTP POST/GET Requests

    Hello, as requested, I'm trying to find a simple form that will upload a file to a server using a username and password. If I can hard code the specifics and click one button, that would be great. The current form i'm using works, however, when we set up the server to use credentials, we keep gettings erros. So i'm searching for a smipler way to accomplish this. I will include the current code.



    Error that is coming from the form:

    "The Server returned an invalid or unrecognized response."




    Error coming from the server side is:


    "Invalid method in request –-Xu02=$"





    (I omitted username, password, and server location)

    Code:
    ----------------------------FORM CODE-----------------------------------
    
    
    
    
    Private Sub Command1_Click()
    
    Dim iArray
    
    'Set your filename to upload here
    iArray = Array("C:\Documents and Settings\rdstephens\Desktop\Upload.txt")
    
    'Set path to php upload script here
    Text1.Text = UploadFiles(iArray, "--------------Server Address--------------", Text2.Text, "******", "******")
    
    End Sub
    
    
    
    
    
    
    
    
    
    ----------------------------SCRIPT CODE------------------------------------
    
    
    
    'You need reference to Microsoft WinHTTP Services 5.0 or 5.1 to use this example
    
    'Credit to Joseph Z. Xu ()
    'Modified by Mohd Idzuan Alias () August 18, 2004
    'Modified by Kung Fu Panda, Wizard of Warcraft, and The Intern, June 19, 2008
    
    
    Dim WinHttpReq As WinHttp.WinHttpRequest
    Const HTTPREQUEST_SETCREDENTIALS_FOR_SERVER = 0
    Const HTTPREQUEST_SETCREDENTIALS_FOR_PROXY = 1
    Const BOUNDARY = "Xu02=$"
    Const HEADER = "--Xu02=$"
    Const FOOTER = "--Xu02=$--"
    
    
    Function UploadFiles(strFileName As Variant, strURL As String, Optional postVar As  String, _ Optional strUserName As String, Optional strPassword As String) As String
    
    Dim fname As String
    Dim strFile As String
    Dim strBody As String
    Dim aPostBody() As Byte
    Dim nFile As Integer
    
    Set WinHttpReq = New WinHttpRequest
    
    ' Turn error trapping on
    On Error GoTo SaveErrHandler
    
    ' Assemble an HTTP request.
    strURL = strURL & "?slots=" & CStr(UBound(strFileName) + 1) & "&" & postVar
    WinHttpReq.Open "POST", strURL, False
    Debug.Print strURL
    
    If strUserName <> "" And strPassword <> "" Then
    ' Set the user name and password.
    WinHttpReq.SetCredentials strUserName, strPassword, _
    HTTPREQUEST_SETCREDENTIALS_FOR_SERVER
    End If
    
    '-------------------------- Becareful not to mingle too much here -----------------------------------
    
    ' Set the header
    WinHttpReq.SetRequestHeader "Content-Type", "multipart/form-data; boundary=" & BOUNDARY
    
    ' Assemble the body
    strBody = HEADER                        ' Starting tag
    
    For i = 0 To UBound(strFileName)
    
    ' Grap the name
    fname = strFileName(i)
    
    ' Grap the file
    strFile = getFile(fname)
    
    strBody = strBody & vbCrLf & "Content-Disposition: form-data; name=""" & "name" & """; filename=""" & fname & """ " & vbCrLf & "Content-type: file" & _
    vbCrLf & vbCrLf & strFile & vbCrLf
    
    If i < UBound(strFileName) Then
    strBody = strBody & "--Xu02=$" ' This is boundary tag between two files
    End If
    strFile = ""
    
    Next i
    
    strBody = strBody & FOOTER             ' Ending tag
    '----------------------------------------------------------------------------------------------------
    
    ' Because of binary zeros, post body has to convert to byte array
    aPostBody = StrConv(strBody, vbFromUnicode)
    
    ' Send the HTTP Request.
    WinHttpReq.Send aPostBody
    
    ' Display the status code and response headers.
    'UploadFiles = WinHttpReq.GetAllResponseHeaders & "  " & WinHttpReq.ResponseText
    UploadFiles = WinHttpReq.ResponseText
    
    Set WinHttpReq = Nothing
    
    Exit Function
    
    SaveErrHandler:
    UploadFiles = Err.Description
    
    Set WinHttpReq = Nothing
    
    End Function
    Function getFile(strFileName As String) As String
    
    Dim strFile As String
    
    ' Grap the file
    nFile = FreeFile
    Open strFileName For Binary As #nFile
    strFile = String(LOF(nFile), " ")
    Get #nFile, , strFile
    Close #nFile
    
    getFile = strFile
    
    End Function

  3. #3
    New Member
    Join Date
    Jun 2008
    Posts
    2

    Re: Winsock: Making HTTP POST/GET Requests

    Quote Originally Posted by visualAd
    The problem is caused by the maximum string size in VB6. If it exceeds 32KB it is truncated; I just viewed the Yahoo source coed and it appears that it is about 50KB.

    If you want to be able to use larger strings then you need to use text stream and load the data into either a rich text box or a file.

    Hey sorry, I got so lost in the code that I forgot to come back and say thanks. I loaded the data into the rich text box and it worked out perfectly. So ya, thanks.

  4. #4
    Banned
    Join Date
    Mar 2008
    Posts
    24

    Re: Winsock: Making HTTP POST/GET Requests

    How can i use Cookies
    I Mean i been done Login Packet Its workin HTTP
    now i want took cookies and replce and i can use other packet too with these cookies
    if someone know expln thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width