# VBForums CodeBank > CodeBank - Visual Basic 6 and earlier >  Winsock: Making HTTP POST/GET Requests

## visualAd

*Introduction*

A question often asked is how to use Visual Basic to submit information to server side web scripts. Such as form variables. This small demonstration application shows how to accomplish this with the use of VB6 and the WinSock control.

In order to add the Winsock Control to your VB6 application, right click on the tool box, select components from the menu and check the box "Microsoft Winsock Control".

*A Bit about HTTP*

The Winsock control enables you to establish a connection to another computer using either TCP / UDP and transfer data over that connection. HTTP (Hypertext Transfer Protocol) is used to request and send data to web resources and it is up to the programmer to construct the HTTP request and parse the response.

There are two types of HTTP request, a POST request and a GET request. A POST request is used to send data to the server resource and a GET request is used to retrieve data from the server.

Data can also be passed to the server resource by means of a query string in both GET and POST requests. The query string is appended to the end of a URL in the form:

[http://www.domain.com/resource.php?variable1=value&variable2=value



*Using the Demo Application*

The demo application allows you to enter a URL and choose between a HTTP GET and HTTP POST request. The variable's section gives you the opportunity to send additional variables along with the HTTP request.

*Variables*
If you choose the GET method these variables will be added to the URL's query string. If you choose a POST request, they will be put into the body of the HTTP request.

*Headers*
The headers section allows you to add some custom HTTP headers, such as a user agent and cookies.

*Sending the Request*
Once the request has been sent you will be able to see the exact HTTP request sent to the server. After a short time you will also see the HTTP response.

You can test the application using the following URL:

http://adam.codedv.com/examples/post_dump.php

----------


## tony007

Many thanks for sharing this cool program with us. I tried to use your code with one of my project with involves loading url from listbox in submission variables value textbox. So i added a listbox that get populated from mysql db. I added a button so that on click it starts looping through listbox item and take each item and put it in submission variables value textbox and click send. What ever i do i can make this working with loop . I encluded the code for my click button but it never works. Could u help me fix this problem.thanks



VB Code:
' transfer all urls i loop
Private Sub Command1_Click()
 Dim i As Long
For i = 0 To List2.ListCount - 1
 List2.Selected(i) = True
txtVariableValue(0) = List2.List(i)
'MsgBox List2.List(i)
cmdSend_Click
Next
 End Sub

----------


## visualAd

I am sorry, you are going to have to make yourself clearer.

----------


## tony007

> I am sorry, you are going to have to make yourself clearer.


This is what i want to do :

----------


## Rich2189

You can also post to webpages using the inet control



```
Private Sub cmdAdd_Click()
Dim strUrl As String
    Dim strFormData As String
    strUrl = ""

    strFormData = "sql=SELECT  FROM  WHERE "


   
    
    Inet1.URL = strUrl
    Inet1.Execute strUrl, "post", strFormData, "Content-Type: application/x-www-form-urlencoded"
End Sub

Private Sub Inet1_StateChanged(ByVal State As Integer)
    
    
    
    Select Case State
    
    Case icConnecting
    Case icConnected
    Case icRequesting
    Case icRequestSent
    Case icReceivingResponse
    Case icResponseReceived
    
    Case icResponseCompleted
        
        Dim strTemp As String
        strTemp = Inet1.GetChunk(128)
    
        strTemp = Trim(strTemp)
        If InStr(1, strTemp, "0:") Then
            MsgBox "error: " & strTemp
        Else
            MsgBox strTemp
        End If
    
    End Select

End Sub
```

Where you have a post variable called sql, and obviously an online database set up. This example works fantastic with a php script i have on a server.

Rich

----------


## tony007

Rich thank for u reply. could u be more specific how to use the code like what controles i need and ... 
*Your code does not post back the result of post request*  and also taking value from sql directly will not do what i want. Since i want to execute a list of values and sql will not be able to fetch them one by one .
Therefore i populate the values in a listbox first then i want to the process them one by one so i be happy if u show me how that can be done either your code or visualAd code.

My main goal is to be able to do the following :

In *textbox1* where i put the post url such as : http://localhost/scripts/script.php

then 2 small text box(*textbox1 and textbox2*) one for the value i want to send to my script and and value name and a *listbox,textbox4*  and *submit button*. 

I want to be able to type values in textbox1 and textbox 2 and click submit and the result get output in my textbox4.*Also i want to be able to automate the process so instead of me having to  type value to textbox3 which holds value to be passed to post , Instad i click on button and the program takes first item of listbox and put it in textbox3 and output the result in textbox4 and with  that finishes do the same for the rest of items in listbox.* The only part that i could not make it work in virutal ad project is to make it automated and so far i am not getting any help fixing it.I hope some one be kind of enough to help me here.Thanks

Note the pic above shows what i want.

----------


## Rich2189

You need to add the microsoft internet controls to your project. Well basically you post to the script and it will echo the result to the screen. This is where the getchunk method comes in, it gets the echo that the php script produces. As for the inputs, make the url = to textbox1. and sql or what ever your post variable is called = to textbox2. then stick the cmdadd code into your submit button. 

Yes your best bet would be to return all your values at once to your VB program and store them in an array. Then process them one by one.

Rich

----------


## tony007

> You need to add the microsoft internet controls to your project. Well basically you post to the script and it will echo the result to the screen. This is where the getchunk method comes in, it gets the echo that the php script produces. As for the inputs, make the url = to textbox1. and sql or what ever your post variable is called = to textbox2. then stick the cmdadd code into your submit button. 
> 
> Yes your best bet would be to return all your values at once to your VB program and store them in an array. Then process them one by one.
> 
> Rich


Could u tell me how to display what ever the post returns back to me too. This only shows me :

<form method=post action="myscript.php">
 Link: <nput type ="tet" name="link2" size="35">&nbsp;
<input type="submit" value="c

The above infor is useless. For example if i want to make a post to php script i need to send *value name*  and *value data*  and i will get a result out that ok your file is submitted. *But your code does not send value name and it only sends value data!!* and also no result out in textbox!! could u show me how i can get the script output in a textbox.Thanks

----------


## sufyaaan

> *Introduction*
> 
> A question often asked is how to use Visual Basic to submit information to server side web scripts. Such as form variables. This small demonstration application shows how to accomplish this with the use of VB6 and the WinSock control.
> 
> In order to add the Winsock Control to your VB6 application, right click on the tool box, select components from the menu and check the box "Microsoft Winsock Control".
> 
> *A Bit about HTTP*
> 
> The Winsock control enables you to establish a connection to another computer using either TCP / UDP and transfer data over that connection. HTTP (Hypertext Transfer Protocol) is used to request and send data to web resources and it is up to the programmer to construct the HTTP request and parse the response.
> ...


 It doesnt work on *https*  sites. How can I tweak it to work on them as well?

----------


## DrGonzo

HTTPS means HyperText Transfer Protocol Secure. 
That is HTTP encrypted with SSL.
Due to my little knowledge i suggest you to read the Wikipedia article about HTTPS. 
There you will probably find a link to the RFC or other ressources which may help you.

GERMAN: (with a graphic)
http://de.wikipedia.org/wiki/HTTPS

ENGLISH: (without any graphic)
http://en.wikipedia.org/wiki/HTTPS

----------


## indianbill007

Dear sir,
I was trying to post this form 

https://login.yahoo.com/config/login?
 to automatically login into the yahoo account using this project, but it gave 
Invalid protocol schema error.
Can you please help me out with this.
Regards
Sumit Ghosh
Chief Software Architect
Globussoft
www.globussoft.net

----------


## DrGonzo

I think because you want to access a site using HTTPS (HTTP encrypted by SSL) your program must have also implemented SSL to encrypt your HTML-data and for further communication between the client and the server.  :Ehh:  

 :wave:

----------


## feneck

Hello. Would this program be able to work for MySpace?

----------


## feneck

Oh, dont worry about my last comment, i have played with it and have got it to work. thank you all very much!!!!!!!!!!

----------


## jackmoros

I am trying to automatically download data from a site. In order to access some pages of the site i need to login. To login i have to submit user and password via POST. Using this form i have been able to properly login the site, setting my user and passrowd. Now how can i access the other pages of the site? 
This is the response to my login, as appears in the Winsock Form Submission:

_HTTP/1.1 302 Found
Date: Sun, 27 May 2007 11:39:20 GMT
Server: Apache/2.0.58 (Unix) mod_ssl/2.0.58 OpenSSL/0.9.7i
X-Powered-By: PHP/5.1.4
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: PHPSESSID=824066ab9840f2aebe9f46b402a0c5df; path=/
Set-Cookie: cookielogin=%2C3183; expires=Mon, 26-May-2008 11:39:20 GMT
Location: club.php
Vary: Accept-Encoding,User-Agent
Content-Length: 0
Connection: close_
The login is successful, but now i don't know how to download data from example from the page club.php. The name of the site is: www.rugbymania.net

Thank you

----------


## Pitoniso

Jack i have other problem with do a keepalive connection...

Once I pass to the proxy the petition Post that I have to send to the proxy so that I don't close the connection, because I want to continue passing data, you know something about this

----------


## jackmoros

i'm sorry but, i didn't understand what you mean.
Could explain that again, please?

----------


## Pitoniso

I want to establish a connection among 2 programs using a single petition POST, how i could make this... that the beginning of the connection is a petition POST..

----------


## Trovamaster

Hi people,

I already try the code and modified to fit my requirements.

It work very well thanks to visualAd, but i have a little problem with the control winsockt, i duno how to ensure the control propely connect or disconect.

It works great, but at some point the control dosent disconect or conecct and the code hang up, into an infinite cilce, in part 1 or part 2 (listed in the code above)

i already try a lot of things, and of course i have 2 event's that change the flags for conected or disconected, on sckts events.

i hope somebody can help to figure out this issue.

Sorry for my very bad english.

this is part of the code:

lngIndice = 1
While lngIndice <= lngCantidadArchivos

' if the conection is un use, wait until disconected
' (part 1 when the control don't disconnect propely)
If Not mbolSckConectado Then

' Here i set variables for teh file
strNombreArchivo = arsNumerosOV(lngIndice) & ".PDF"
strArchivoPDF = strDirectorio & strNombreArchivo

' here i get the contents of file
strContenidoArchivo=dfObtenerContenidoArchivo(strArchivoPDF)

' here i biuld the HTTP request
strRequisicionHTTP = dfHTTPRequest(strContenidoArchivo, udtURLDestino, strNombreArchivo, strNombreArchivo, strTipoMIME)

' here i conecct to the server, and wait until conected
' (part 2 when the control don't connect propely)
sckSitioWeb.Connect
While Not mbolSckConectado
DoEvents
Wend

' here i send the HTTP request
sckSitioWeb.SendData strRequisicionHTTP

' here i susspend the program ejecution 2 sec
dfSuspenderEjecucion 2

End If

Wend

----------


## Dustcrazy

EDITED: I fixed it  :Blush:  

Thank you..

----------


## Xavier09

Hi. I'm new to this forum and using Visual Basic. I'm currently using Visual Basic 6.0 to write my applications. I understood this small tutorial in this thread really well, but have a few questions. I'd like to know how to request a site's full HTML source code for instance www.yahoo.com without it returning only little bits of the code.


I did try requesting the page using Inet and Winsock, but for some reason it only returns half of the source code and not the full page. Is there anyway to retrieve the whole HTML source?

Any reply would be gladdy appreciated. Thanks!  :Big Grin:

----------


## 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.

----------


## rich_19

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)



```
----------------------------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
```

----------


## visualAd

HTTP authentication is quite simple. A normal request is made to the HTTP server and it will respond with a 401 unauthorized response. The response will contain a WWW-Authenticate header which will include the authentication scheme supported and the name of the realm.



```
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Basic realm="Authorized Users"
```

The client must then make a new request an authorization header. This head contains the name of the authentication scheme and a base64 encoded user name and password in the form:



```
' null character to separate username and password.
authorization = FunctionToBase64Encode(username & ":" & password)
```

The second request may look something like this:


```
GET /protected.html HTTP/1.0
Host: www.example.com
Authorization: Basic dmlzdWFsYWQ6c2VjcmV0
```

You can pick some source code up for a Base64 encoder / decoder easily from a Google search. Other authentication schemes include digest and NTLM. Digest is relatively simple to implement however, NTLM is largely undocumented and insecure for use over the Internet.

----------


## rich_19

I sort of understand what you are saying, but not sure I could implement that into a working form. So if the above code won't work, do you know of a simpler program that I can input the server, username, and password, that will work?

----------


## visualAd

> I sort of understand what you are saying, but not sure I could implement that into a working form. So if the above code won't work, do you know of a simpler program that I can input the server, username, and password, that will work?


That is simple  :Confused: , you just need to modify the application to include the extra headers.

----------


## Xavier09

> 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.  :Big Grin:

----------


## angel.girl

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

----------


## visualAd

You will see a Set-Cookie header in the HTTP response. The next request you make should contain the name and the value of the cookie in a cookie header.


```
Request:

GET / HTTP/1.0

Response:

HTTP/1.0 200 OK
Set-cookie: cookie1=value1; domain=www.example.com; path=/blah
Set-Cookie: cookie2=value2; path=/

Next Request:

GET /blah/index.html HTTP/1.0
Cookie: cookie1=value1; cookie2=value2
```

----------


## batori

hi,well this is a great post but i got a question

well when i do the operation with your tool i get the code,then i paste it to a text file and then rename it to .html.Then i open the file and i get the page i wanted and it says that im logged in (which is great  :Big Grin:  ) and then automatically it takes me to my documents (which is not so important)

Then i copied the code that your tool produced



```
http://www.testforum.org/login.php?username=batori&password=batori&autologin=on&login=Log%2Bin
```

and i pasted it in here



```
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private Sub Form_Load()

Call ShellExecute(0&, vbNullString, "http://www.forum.org/login.php?username=batori&password=batori&autologin=on&login=Log%2Bin", vbNullString, vbNullString, vbNormalFocus)

End Sub
```

then my browser open and then i get the message that the specified account does not exist??? hmmmm :Frown:  

I tought the reason could be the cookies...
because after i login i must go automatically to a new thread and post the text into a REPLY THREAD and click SUBMIT automatically...

----------


## visualAd

I am not sure what you want to achieve but I if you had copied and pasted it, then surely the passwords wouldn't be different and the website would be the same  :Ehh: . Do you want to post passwords to a web site on a public forum too?

Anyhow, it looks like the error you are getting is caused by the fact that the account does not exist on the forum.

----------


## batori

well, when i get this code :

this is the request


```
POST /login.php HTTP/1.0
Host: testforum.org
Content-Type: application/x-www-form-urlencoded
Content-Length: 59

username=batori&password=batori&autologin=on&login=Log%2Bin
```

and i just used this in my app and i got the error that the account does not exists.i wanted to open this code in the url like i posted before.but it doesnt work


```
username=batori&password=batori&autologin=on&login=Log%2Bin
```

but when i generated the code with your app...i pasted that in a html file and i opened it and i was successfully logged in...

so now im askin..is this not woking in my case because i just tried to open that url


```
www.testforum.org/login.php?username=batori&password=batori&autologin=on&login=Log%2Bin
```

instead of sending



```
POST /login.php HTTP/1.0
Host: testforum.org
Content-Type: application/x-www-form-urlencoded
Content-Length: 59

username=batori&password=batori&autologin=on&login=Log%2Bin
```

----------


## visualAd

I suggest you read up a little on the HTTP protocol. The application I wrote shows you the HTTP request. It is a post request and the url encoded data is sent in the body of the HTTP request (as shown by the application).

You cannot append the data to the end of a URL and expect it to work. Not only this, the sites you are using have two different forum packages that likely accept login information in a different format, so even if the above were possible, it would probably still fail.

----------


## batori

ah well ok...becaus a friend managed to do something like that in delphi in a veery small piece od code...


```
http://www.vbforums.com/showthread.php?t=537443
```

----------


## indianeagle

thanks sir 4 ur demo example...
i leared too much from that
altho it took almost 6 hrs to understand whole program
bt fianlly got success in making a simple sending data by POST method.. same as i wanted

now my doubts r as follows --

1.in second text box i wana get Message..
   for that i need txt box to be of multiline
   but wen i post data with multiline property 'true' it started showing sum chr like %D%A etc.. also its behaviour is strange for sum keys like "",',%,& etc.... they doesnt get psoted in same manner.... wat php code sud be used to get them properly
2. how much size of msg can b posted... means if i want 5mb text then will it support... or will crash?
3. wat does (htmlspecialchars($name)) signifies in ur php file?
4. i learned to use POST function using winsock .. bt can it also be possible using inet control... if yes than which one is btr?
5. wat does ur following code mean for.. n to get decode these changes in php file (m nt concerning to decode in VB)



```
' ' url encodes a string
Function URLEncode(ByVal str As String) As String
        Dim intLen As Integer
        Dim X As Integer
        Dim curChar As Long
                Dim newStr As String
                intLen = Len(str)
        newStr = ""
                        For X = 1 To intLen
            curChar = Asc(Mid$(str, X, 1))
            
            If (curChar < 48 Or curChar > 57) And _
                (curChar < 65 Or curChar > 90) And _
                (curChar < 97 Or curChar > 122) Then
                                newStr = newStr & "%" & Hex(curChar)
            Else
                newStr = newStr & Chr(curChar)
            End If
        Next X
        
        URLEncode = newStr
End Function
```

hope u get all my doubts
thx

----------


## indianeagle

sorry 4 cretaing new post...
it created by mistake
and now m nt getting delete option

----------


## ck_y

ok but how do you submit a file as in the example here 



> Sending the Request
> Once the request has been sent you will be able to see the exact HTTP request sent to the server. After a short time you will also see the HTTP response.
> 
> You can test the application using the following URL:
> 
> http://adam.codedv.com/examples/post_dump.php



the page here shows a file input box that is file1 
useing the program and sampel source if you supply file1 with an path and file it shows as a varable that was included not as a file that was submited  and is there a work around to this to simply submit a file from a url string?????

i have been unabel to find anythign besides a web page that will work with

<form action="/cgi-bin/upload.cgi" method="post" enctype="multipart/form-data"> 
<input type="File" name="FILE1" size="20">
<p>
<input type="Submit" value="submit">
</form>

----------


## kal_leo@hotmail.com

Hi friends I have alike problem.

what I want to do is I want to login a web page from my vb program
and I don't know how to login. I am not the web master or owner of the site, just a member and I want to login from my vb form. 

I want to pass parameters my username and password.

can somebody pls help me out. I need this badly.

Thanks in advance.

Kal

----------


## visualAd

Please start you own thread for support on issues not relating to the above program.

----------


## batori

Great post ...  :Big Grin:  i already learned some stuff with your app...

But i was wondering...
Once that i got all the cookie data and stuff with logging in...

How do i manage to send that cookie data with another post request?
I tried with inet1.execute.... but wont work  :Frown: 


Thanks again for this tutorial

----------


## ahostbr

> Great post ...  i already learned some stuff with your app...
> 
> But i was wondering...
> Once that i got all the cookie data and stuff with logging in...
> 
> How do i manage to send that cookie data with another post request?
> I tried with inet1.execute.... but wont work 
> 
> 
> Thanks again for this tutorial


store it in a global var...

----------


## motorway

Hi.
Help!!! Some symbols like + and = don't send using WinHTTPRequest.Send
And how to send multiple variables?

----------


## Under_Developed

Thank you very much for the tutorial. This is the only place I could find that had a tut about the winsock and http.

I have made a new thread about this problem *here* if you would be so kind as to read it

I can log into phpbb forums but when I try to post I get 

*Invalid Session. Please resubmit the form.*

I'm sending the following to the server:




> POST  /community/posting.php?mode=post&f=26&sid=e5ab54e065e67b736f0e04e30287e880 HTTP/1.0
> Host: www.phpbb.com
> Accept: "/"
> Content-Type: application/x-www-form-urlencoded
> Referer: http://www.phpbb.com/community/ucp.php
> Content-Length: 54
> Connection: Keep-Alive
> Cache-Control: No-Cache
> Cookie: phpbb3mysql_data=a%3A2%3A%7Bs%3A11%3A%22autologinid%22%3Bs%3A0%3A%22%22%3Bs%3A6%3A%22userid%22%3Bs%3  A5%3A%2286360%22%3B%7D; phpbb3mysql_sid=e5ab54e065e67b736f0e04e30287e880
> ...

----------


## DigiRev

> Thank you very much for the tutorial. This is the only place I could find that had a tut about the winsock and http.
> 
> I have made a new thread about this problem *here* if you would be so kind as to read it
> 
> I can log into phpbb forums but when I try to post I get 
> 
> *Invalid Session. Please resubmit the form.*
> 
> I'm sending the following to the server:


Probably sending a bad/expired session id.




> Cookie: phpbb3mysql_data=a%3A2%3A%7Bs%3A11%3A%22autologinid%22%3Bs%3A0%3A%22%22%3Bs%3A6%3A%22userid%22%3Bs%3 A5%3A%2286360%22%3B%7D; phpbb3mysql_sid=e5ab54e065e67b736f0e04e30287e880


You'll need to parse out the cookie and session id that the server sends back to you, put it in a variable, and send it with future GET/POST requests.

----------


## Under_Developed

Thank you for the reply will try that.

----------


## system track

Hi,
how attach jpg image in your code
please help

----------


## HookahSchwa

Great program, I'm using winsock and I'm running into a problem and I noticed your program does the same thing

In your program, use the GET method and go to

news.yahoo.com

when the data comes in, scroll all the way to the bottom of the data,
you'll notice that it is truncated.  This is just an example, there are tons of other web sites that this happens to too.

I've been getting headaches on how to fix this problem, I need to be able to retrieve web page data with winsock.  Got any ideas?

----------


## visualAd

There is a limit of 64k to the length of strings in Visual Basic 6 and a 32k limit on the text box. If you want to display everything then consider the rich text control and retrieve the response from the server in 1000 byte chunks and append to the control.

This program is only a demonstration of how to format the requests so you will need to make modifications to the code to make it do what you require.

----------


## HookahSchwa

Do you have an example of how do set the winsock control to receive data in chunks?

----------


## HookahSchwa

Nevermind I found the maxlen argument  :Smilie:  thank you for explaining what that problem was!

----------


## whatsup

with this program
is it possible to login to hotmail account.
i mean as i use the explorer to login through the login page.
i want to do that with a vb app, not with the explorer.
is it possible with this program ?
if yes, an example code would be much appreciated.

----------


## nqmam

The URL Encode function is wrong.
If the character code is less than 16 the hex code is only one symbol. It must be with a leading zero. Might be fixed this way:



```
If curChar < 16 Then
    newStr = newStr & "%0" & Hex(curChar)
Else
    newStr = newStr & "%" & Hex(curChar)
End If
```

Or this way:



```
newStr = newStr & "%" & Right("0" & Hex(curChar), 2)
```

----------


## whatsup

hay,
i attached a zip, which contain a text file i got from:
live server on "login.live.com"
this is a html file of the login page, that i managed retrieve via winsock,
but i don't know how to continue,
what the exact text i need to send back, with my username and password,

can someone please post the exact string i need to send back to the server
with my username and password.

you can write in the username: whatsup
in the password: 00000000 
so i can find exactly what you did.

thanks in advance.

----------


## rohini409

Hello All,

I am new to this forum, this is my first post and I need help  :Frown: 

I just downloaded the zip package from first post. Attempted to run the project on MS Visual Studio 2005, which attempted to convert the project from VB6 to VB2005 . Now, when I run the application and do exactly what I am supposed to do, the request and output boxes confirm that, it communicates to the server as http://hostname/myfile.php?var1=xx&var2=yy&var3=zz but, no data passes through. 

Though I made some modifications , I now have no manual modifications (except from automatic upgrade). Would anyone please help me by telling what could possibly be wrong?

In case needed, my post_dump.php is as follows:

```
<?php 

include_once("dbconnect.php");
date_default_timezone_set('Europe/London');
$Day=date("d-m-y H:i");
if ($_GET): 
	foreach($_GET as $name => $var) :  
		$store_array[]=$var;
	endforeach; 
endif;
$var1=$store_array[0];
$var2=$store_array[1];
$var3=$store_array[2];
mysql_query("INSERT INTO `table` (var1,var2,var3,Day)VALUES ('$var1','$var2','$var3','$Day')");
?>
```

----------


## si_the_geek

Welcome to VBForums  :wave: 

Attempting to use VB6 code in VB.Net (VB 2002 and later) is a bad idea, as not only does it take extra effort to get it working, but it is also likely to be much less efficient than the VB.Net methods of achieving the same tasks.

I recommend you find the equivalent in our VB.Net CodeBank forum instead.

----------


## vvarun

i hve 1 doubt

like i log in in some site using post/get Now i done successfully Now my problem is i want to visit user profile page after login my id

how can i do tht ?
thnx

----------


## akhileshbc

> i hve 1 doubt
> 
> like i log in in some site using post/get Now i done successfully Now my problem is i want to visit user profile page after login my id
> 
> how can i do tht ?
> thnx


Hi Varun... Welcome to the forums... :wave: 

I think you need to store the cookies inorder to continue viewing the user profile. But I'm not sure about that  :Smilie:

----------


## Noob13

Thankx for demo and code
sorry I`m not so good in English
how can make accept sll
I want try to login to myspace
I don't know if I need cookies store too!?

Thankx for help

----------


## sallong.27

hi sir,
 i understand vb code. thanks for ur code. i need one help in this section.
how to get this variable in php page?
pls give me sample?

----------


## Ufkabakan

I will post 2 request to same remotehost without winsock.close or winsock.state 8.

First post working, but in second post VB said to me Runtime error 400006, wrong connection state. I was removed "winsock.Close" command in "Private Sub winsock_Close()" and removed/modifyed blnConnected checking.

How to stay connection for using PHP Session id with cookie?

EDIT: RESOLVED, I dont need stay connection. I can use same Session ID with next cookie.

@sallong.27
You must look webpages source code in/near <form> tag. If u want write a tool for using some get/post webpage action, u must learn some basic HTML codes.

----------


## Ufkabakan

URL Encode function is inadequate for UTF8 sybols/chars like Turkish and Chines and Arabic words. I have a resolution
Example:


```
Print "ABcdığ !+"
Print URLEncode("ABcdığ !+")
Print URLEncode2("ABcdığ !+")
```

Result


```
ABcdığ !+
ABcd%FD%F0%FC%20%21%2B
ABcd%C4%B1%C4%9F%C3%BC%20%21%2B
```

General Declaratetions


```
Private Declare Function WideCharToMultiByte Lib "kernel32" ( _
ByVal CodePage As Long, ByVal dwFlags As Long, _
lpWideCharStr As Any, ByVal cchWideChar As Long, _
lpMultiByteStr As Any, ByVal cchMultiByte As Long, _
ByVal lpDefaultChar As Long, lpUsedDefaultChar As Long) As Long
```

Modul


```
Public Function URLEncode2(ByRef Text As String) As String
Dim UTF8Bytes() As Byte

    Dim lngA As Long, strChar As String
    For lngA = 1 To Len(Text)
        strChar = Mid$(Text, lngA, 1)
        If strChar Like "[A-Za-z0-9]" Then
        Else
           If strChar = "=" Then
           Else
            W2UTF8 strChar, UTF8Bytes
            strChar = HexEncode(UTF8Bytes)
           End If
        End If
        URLEncode2 = URLEncode2 & strChar
    Next lngA
End Function


Function W2UTF8(W As String, UTF8() As Byte) As Long
Dim Bytes&
If LenB(W) = 0 Then Exit Function
ReDim UTF8(LenB(W) + 16)
Bytes = WideCharToMultiByte(65001, 0, ByVal StrPtr(W), Len(W), _
UTF8(0), UBound(UTF8), 0, ByVal 0&)

ReDim Preserve UTF8(Bytes - 1)
W2UTF8 = Bytes
End Function

Function HexEncode(B() As Byte)
Dim i&, HexArr() As String
ReDim HexArr(UBound(B))
For i = 0 To UBound(B)
HexArr(i) = Right$("0" & Hex$(B(i)), 2)
Next i
HexEncode = "%" & Join(HexArr, "%")
End Function
```

And i thing we dont need  "blnConnected" for checking connection/closed etc. Winsock.state have a state parameter for it:

WinSock.State = sckClosed = 0
WinSock.State = sckOpen = 1
WinSock.State = sckListening = 2
WinSock.State = sckConnectionPending = 3
WinSock.State = sckResolvingHost = 4
WinSock.State = sckHostResolved = 5
WinSock.State = sckConnecting = 6
WinSock.State = sckConnected = 7
WinSock.State = sckClosing = 8
WinSock.State = sckError = 9

Example:
Waiting Close connection before new connection:


```
        Do Until winsock.State = 0
        DoEvents
        Loop
```

----------


## naruponk

How can I use to send utf-8 value request to http ?
The php page give me the result of variable I sent as ???

----------


## Ufkabakan

@naruponk
Im sory i cant understand, what do u want clearly. This UTF-8 function can URL encoding for unicode strings like UTF-8 Russian or Greak or Turkish words.

----------


## FeaRiTSeLF

Can someone please help me do this exact thing but with multiple urls?? My vb is poor.

----------


## romanos8

Some one can upgrade this code to vb 2010? If yes it will be very helpfull!

----------


## Nightwalker83

> Some one can upgrade this code to vb 2010? If yes it will be very helpfull!


It would probably be better if you rewrite it for use with Visual Basic .NET.

----------


## bulleyes

hello! 

how to post a username and password via vb then open the url on firefox? 

for example 

on my vb program

there are username and password textbox

then i enter

when i click the command button ,

it go to the website with my username and password and the url

----------


## drinella

Hello.
Are you still out there to maybe possibly help me with this?  Thanks!

----------


## drinella

I modified this app a bit to simplify it but I'm getting some errors.  When I try the "Send" command button a second time, "Invalid Operation at Current State" error occurs.  
I am also having timeout problems sending multiple requests.  The service is not recording data sent continuosly, hitting the "send" button.  I think it has something to do with Winsock and not the service.  
Would someone mind helping me?  Thanks!


Option Explicit

' we set this to true whil a connection is established
Private blnConnected As Boolean

Dim Transaction_Num As String
Dim customer As String
Dim Location As String
Dim Weight As String
Dim Sort_Class As String
Dim ppp As Variant


Private Sub cmdSend_Click()
    Dim strMethod As String
    Dim strData As String
    Dim strPostData As String
    Dim strHeaders As String

    Dim strHTTP As String
    Dim X As Integer
    Dim tmp As Integer
    Dim Transaction_NumInt As Integer
    Dim thestring As String

    blnConnected = False

    strPostData = ""
    strHeaders = ""

    If blnConnected Then Exit Sub

'"GET
'For all loads: http://ics.etechsystems.com:3000/col..._loads_on_belt   (test) or http://192.168.0.21:3000/colmac/current_loads_on_belt   (on site)
'To query a single load:  http:// ics.etechsystems.com:3000/colmac/get_load/x (test) or   http:// 192.168.0.21:3000/colmac/get_load/x  (on site)
'For this service the x should be replaced with the tranaction_number you would like to find.
'    txtUrl.Text = "http://ics.etechsystems.com:3000/colmac/current_loads_on_belt"
'    txtUrl.Text = "http://ics.etechsystems.com:3000/colmac/get_load/1"

'POST
'For posting a load during a Colmac dump on belt:   http://ics.etechsystems.com:3000/colmac/new_load   on site:  http://192.168.0.21:3000/colmac/new_load

    ' configure winsock
    winsock.Protocol = sckTCPProtocol
    winsock.RemoteHost = "ics.etechsystems.com"
    winsock.RemotePort = 3000

    strMethod = "POST"

    ' build encoded data the data is url encoded in the form
    strData = ""
    strHTTP = strMethod & " " & "/colmac/new_load" & " HTTP/1.1" & vbCrLf
    strHTTP = strHTTP & "Host: " & "ics.etechsystems.com" & vbCrLf

    Transaction_Num = 304

        Transaction_NumInt = Val(Transaction_Num)
        Transaction_Num = str(Transaction_NumInt)
        customer = "5"
        Location = "2"
        Weight = "3"
        Sort_Class = "4"


        '- See above: Entered the URL http://ics.etechsystems.com:3000/colmac/new_load
        strPostData = strPostData & "<new_load>" & vbCrLf
        strPostData = strPostData & "<transaction_number>" & Transaction_Num & "</transaction_number>" & vbCrLf
        strPostData = strPostData & "<customer>" & customer & "</customer>" & vbCrLf
        strPostData = strPostData & "<location>" & Location & "</location>" & vbCrLf
        strPostData = strPostData & "<weight>" & Weight & "</weight>" & vbCrLf
        strPostData = strPostData & "<sort_class>" & Sort_Class & "</sort_class>" & vbCrLf

        strPostData = strPostData & "</new_load>" & vbCrLf

        strHeaders = "Content-Type:text/xml" & vbCrLf & _
                                 "Content-Length:" & Len(strPostData) & vbCrLf

        strHTTP = strHTTP & strHeaders
        strHTTP = strHTTP & vbCrLf
        strHTTP = strHTTP & strPostData

        txtRequest.Text = strHTTP

        winsock.Connect

        ' wait for a connection
        While Not blnConnected
            DoEvents
        Wend

        ' send the HTTP request
        winsock.SendData strHTTP

thestring = "C:\Program Files\Internet Explorer\IEXPLORE.EXE  http://ics.etechsystems.com:3000/colmac/get_load/" + Transaction_Num
Shell thestring

txtResponse = "Transaction Number:  " + Transaction_Num

        Transaction_Num = str(Transaction_NumInt + 1)

End Sub

 Sub winsock_Connect()
    blnConnected = True
End Sub

----------


## HANI619

help me

i wont to mack my forum poster in vb6 with winsock tool.

i login the vb forum with this code  :Thumb: 


```
Private Sub CommandLogin_Click()
Dim Packet As String
Dim Post As String
Post = "vb_login_username=" & Txtuser & "&vb_login_password=" & Txtpass & "&s=&securitytoken=guest&do=login"
Packet = "POST /login.php?do=login HTTP/1.1" & vbNewLine
Packet = Packet & "Host: www.absba.org" & vbNewLine
Packet = Packet & "User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:16.0) Gecko/20100101 Firefox/16.0" & vbNewLine
Packet = Packet & "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" & vbNewLine
Packet = Packet & "Accept-Language: ar,en-us;q=0.7,en;q=0.3" & vbNewLine
Packet = Packet & "Connection: keep -alive" & vbNewLine
Packet = Packet & "Content-Type: application/x-www-form-urlencoded" & vbNewLine
Packet = Packet & "Content-Length: " & Len(Post) & vbNewLine & vbNewLine & Post
 
 If Winsock.State = 7 Then Winsock.SendData (Packet)
    
End Sub
```


but i cant post my Thread by this code 



```
Private Sub CommandNewThread_Click()
Dim Packet As String
Dim Post As String
Post = "subject=" & Text1 & "&message=" & Text2 & "&wysiwyg=1&iconid=0&f=29&do=postthread&posthash=&poststarttime=&loggedinuser=1026648&sbutton=&parseurl=1&emailupdate=1"
Packet = "POST /newthread.php?do=postthread&f=2 HTTP/1.1" & vbNewLine
Packet = Packet & "Host: www.absba.org" & vbNewLine
Packet = Packet & "User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:16.0) Gecko/20100101 Firefox/16.0" & vbNewLine
Packet = Packet & "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" & vbNewLine
Packet = Packet & "Accept-Language: ar,en-us;q=0.7,en;q=0.3" & vbNewLine
Packet = Packet & "Connection: keep -alive" & vbNewLine
Packet = Packet & "Content-Type: application/x-www-form-urlencoded" & vbNewLine
Packet = Packet & "Content-Length: " & Len(Post) & vbNewLine & vbNewLine & Post
 
 If Winsock.State = 7 Then Winsock.SendData (Packet)
```

thes is the code of http headerrs..



```
http://www.absba.org/newthread.php?do=postthread&f=29
POST /newthread.php?do=postthread&f=29 HTTP/1.1
Host: www.absba.org
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:16.0) Gecko/20100101 Firefox/16.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: ar,en-us;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: http://www.absba.org/newthread.php?do=newthread&f=29
Cookie: bbsessionhash=U-MUbRgiFfdH8CrOlwP67lq8GJKqM3Igc3oDs41kOa8aj4L2gWVKP6yX55dmVNYC; bblastvisit=8dzKEZexLzFgjulJQXO19QSy8xlkBYQ1bsbsuTIiuL0.; bblastactivity=74eMV7PnXeHIAPCBvldA_77lSqVIMSJZ18_d8uexO6M.; __utma=208997954.766644000.1327287630.1327287630.1327287630.1; __utmb=208997954.38.9.1327289126285; __utmc=208997954; __utmz=208997954.1327287630.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); bbforum_view=31_pQ9K5OkHVqpcq_zo8DPYvSjON3yJtS4cq7vlvPh9a-EH1BAmRpPglx5HoPV4KvZo1jbIs0k8L17X2oTEYV4HVb46vM4t2eg-SaFPoEQQ.; bbthread_lastview=tXnsL_aOuY40KNzY0xiOHDG35S0hJQuFQv-XiI1XgBVW0h3NcbP2FwFUDSfvqXtbBIsnOrJpwh7_sRJpF_wHuUWfHDA02Hi0olO9oFk_YPQGzQTtXo_LJRGQtBzCclD2
Content-Type: application/x-www-form-urlencoded
Content-Length: 278
subject=test1&message=test2%3Cbr%3E&wysiwyg=1&iconid=0&s=&securitytoken=1351778534-c987b6ae1f3b93bd32c000d06b175a34ce3972e6&f=29&do=postthread&posthash=&poststarttime=&loggedinuser=1026648&sbutton=%C7%DA%CA%E3%CF+%C7%E1%E3%E6%D6%E6%DA+%C7%E1%CC%CF%ED%CF&parseurl=1&emailupdate=1
```

this is the url
http://www.absba.org/newthread.php?do=postthread&f=29

Please help me with a simple code... :Sick: 
Please help me with a simple code... :Sick: 
Please help me with a simple code... :Sick:

----------


## luka.kecil

thanks U sir,  :wave:

----------


## jhai_salvador

in terms of connection, which is faster? winsock or the ms winhttp 5.1?

----------


## Niya

> in terms of connection, which is faster? winsock or the ms winhttp 5.1?


Both would ultimately be doing the same thing so none would be faster.

----------


## jhai_salvador

> Both would ultimately be doing the same thing so none would be faster.


ohh okay, thanks

----------


## alva0708

> Many thanks for sharing this cool program with us. I tried to use your code with one of my project with involves loading url from listbox in submission variables value textbox. So i added a listbox that get populated from mysql db. I added a button so that on click it starts looping through listbox item and take each item and put it in submission variables value textbox and click send. What ever i do i can make this working with loop . I encluded the code for my click button but it never works. Could u help me fix this problem.thanks
> 
> 
> 
> VB Code:
> ' transfer all urls i loop
Private Sub Command1_Click()
 Dim i As Long
For i = 0 To List2.ListCount - 1
 List2.Selected(i) = True
txtVariableValue(0) = List2.List(i)
'MsgBox List2.List(i)
cmdSend_Click
Next
 End Sub


just add : *winsock_Close* before *cmdSend_Click*. Let me know if it work

----------


## MikiSoft

alva0708, you've just replied to the post which is *10 years* old... let me say it again: T-E-N years
I'm amazed by your grave-digging skills, as I haven't seen such in a while.

----------


## dilettante

Even better: consider how obsolete all of this is.

We now have quite a few helper objects in Windows we can use for making HTTP and HTTPS requests, and HTTP may be on the verge of extinction now in favor of HTTPS which this technique is useless for:

Google Chrome gets ready to mark all HTTP sites as 'bad'

----------


## alva0708

> alva0708, you've just replied to the post which is *10 years* old... let me say it again: T-E-N years
> I'm amazed by your grave-digging skills, as I haven't seen such in a while.


 :Smilie:  i'm a new member of this forum

----------


## alva0708

it work for http and https. just remove http or https when you write url on the textbox. ex. https://www.google.com. you should write *google.com*

----------


## MikiSoft

> i'm a new member of this forum


So what?




> it work for http and https. just remove http or https when you write url on the textbox. ex. https://www.google.com. you should write *google.com*


No, it doesn't. That works because Google still supports HTTP connection, but soon it won't anymore.

----------


## alva0708

Suggestion removed by moderator as inappropriate.

----------


## alva0708

> hello! 
> 
> how to post a username and password via vb then open the url on firefox? 
> 
> for example 
> 
> on my vb program
> 
> there are username and password textbox
> ...


check query string the site that you want to login, and then put in Submission Variables

----------


## MikiSoft

You still keep spamming here with answering on the old posts and even talking about the illegal stuff you do. You seriously want to get banned from here, do you?

----------


## Shaggy Hiker

Do not post suggestions for doing things like this again. Activities like that are against the AUP you agreed to when you signed up.

----------


## alva0708

> Suggestion removed by moderator as inappropriate.





> You still keep spamming here with answering on the old posts and even talking about the illegal stuff you do. You seriously want to get banned from here, do you?



i'm sorry dude....so why old thread did not closed if answered the old post considered a spam ??

----------


## alva0708

never do it again....promise....cheers...

----------


## amnmn

> hi,well this is a great post but i got a question
> 
> well when i do the operation with your tool i get the code,then i paste it to a text file and then rename it to .html.Then i open the file and i get the page i wanted and it says that im logged in (which is great  ) and then automatically it takes me to my documents (which is not so important)
> 
> Then i copied the code that your tool produced
> 
> 
> 
> ```
> ...


It worked for me .Thank you so much . if i wanted to send this http request but don't wanna open the web browser is it possible? it's like kind of get request.

----------

