|
-
Jun 30th, 2008, 10:02 AM
#1
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.
-
Jun 30th, 2008, 10:35 AM
#2
New Member
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
-
Jul 1st, 2008, 10:45 AM
#3
New Member
Re: Winsock: Making HTTP POST/GET Requests
 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.
-
Jul 5th, 2008, 03:26 PM
#4
Banned
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|