HackLife
asked on
VBScript. How to send a POST or GET to a website.
Anyone know how to send a POST or GET to a website using VBScript, such as logging in to the website using the script or sending data to a google search. The point is to send a POST or GET or something similar, Thanks everyone!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Frankly I just use a socket object to open a remote port80 connection and send the post across the socket on connect. I have a hosted vbscript environment that provides a number of additional objects that i use to create toolbars and interfaces, glad to share it, with examples, if you like.
John
John
ASKER
Yes john, something like what you are talking about. I'd like to use a script to logon to a website and then open the page without using sendkeys. I want it to use get or post behind the scenes.
let me see if i can find one of our little "proglets" that uses port 80
john
john
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
gabeso,
XMLHTTP does more than just parse XML documents. It's like an object that does sort of similar things to ASPHTTP.
Everyone,
I'm reading on ASPHttp and XMLHTtp right now to get a better grasp on the objects and what they can provide.
Thanks everyone for helping out.
XMLHTTP does more than just parse XML documents. It's like an object that does sort of similar things to ASPHTTP.
Everyone,
I'm reading on ASPHttp and XMLHTtp right now to get a better grasp on the objects and what they can provide.
Thanks everyone for helping out.
I'm jumping in a little late here, but I think it's important to note that if the use of XMLHTTP is an acceptable solution, then it is by far the easiest way to accomplish POST and GET actions. Source code is below. I think you'll agree that 3-5 lines of code per actions is pretty slick.
Public Function httpGET(url As String) As String
Dim http As New MSXML2.XMLHTTP
http.Open "GET", url, False
http.send
httpGET = http.responseText
End Function
Public Function httpPOST( _
url As String, body As String, _
username As String, _
password As String) As String
Dim http As New MSXML2.XMLHTTP
http.Open "POST", url, False, username, password
http.setRequestHeader _
"Content-Type", _
"application/x-www-form-ur lencoded"
http.send body
httpPOST = http.responseText
End Function
About the only hassle comes from special encoding of strings. The function below will do the trick. I didn't write the original upon which this is based, but can't find any info about who did.
Public Function URLEncode(strInput As String) As String
'
' Convert all "special" characters to %NN style encoding.
' For example, a chr(32) space character is %20
'
Dim sTemp As String
Dim sChar As String
sTemp = ""
sChar = ""
Dim n As Integer
Dim ascii As Integer
For n = 1 To Len(strInput)
ascii = Asc(Mid(strInput, n, 1))
If ascii >= 65 And ascii <= 90 Then ' A..Z
sTemp = sTemp & Chr(ascii)
ElseIf (ascii >= 97) And (ascii <= 122) Then ' a..z
sTemp = sTemp & Chr(ascii)
ElseIf (ascii >= 48) And (ascii <= 57) Then ' 0..9
sTemp = sTemp & Chr(ascii)
Else
sChar = Trim(Hex(ascii))
If ascii <= 15 Then
sTemp = sTemp & "%0" & sChar
Else
sTemp = sTemp & "%" & sChar
End If
End If
Next
URLEncode = sTemp
End Function
-- Craig Yellick
Public Function httpGET(url As String) As String
Dim http As New MSXML2.XMLHTTP
http.Open "GET", url, False
http.send
httpGET = http.responseText
End Function
Public Function httpPOST( _
url As String, body As String, _
username As String, _
password As String) As String
Dim http As New MSXML2.XMLHTTP
http.Open "POST", url, False, username, password
http.setRequestHeader _
"Content-Type", _
"application/x-www-form-ur
http.send body
httpPOST = http.responseText
End Function
About the only hassle comes from special encoding of strings. The function below will do the trick. I didn't write the original upon which this is based, but can't find any info about who did.
Public Function URLEncode(strInput As String) As String
'
' Convert all "special" characters to %NN style encoding.
' For example, a chr(32) space character is %20
'
Dim sTemp As String
Dim sChar As String
sTemp = ""
sChar = ""
Dim n As Integer
Dim ascii As Integer
For n = 1 To Len(strInput)
ascii = Asc(Mid(strInput, n, 1))
If ascii >= 65 And ascii <= 90 Then ' A..Z
sTemp = sTemp & Chr(ascii)
ElseIf (ascii >= 97) And (ascii <= 122) Then ' a..z
sTemp = sTemp & Chr(ascii)
ElseIf (ascii >= 48) And (ascii <= 57) Then ' 0..9
sTemp = sTemp & Chr(ascii)
Else
sChar = Trim(Hex(ascii))
If ascii <= 15 Then
sTemp = sTemp & "%0" & sChar
Else
sTemp = sTemp & "%" & sChar
End If
End If
Next
URLEncode = sTemp
End Function
-- Craig Yellick
ASKER
Very nice. Just what i was looking for. Too bad it's a little late
I would be perfectly content if you want to open a question in CS to give the points for this question to hacklife instead. I was just trying to add my 2cents. I don't care about points here since i'm not maintaining my acct really.
John
John
Hey
Could someone please tell me how to do a HTTP file post using Mutipart , How I should use your function httpPOST for the same. Is there any direct code or function which can help me
Could someone please tell me how to do a HTTP file post using Mutipart , How I should use your function httpPOST for the same. Is there any direct code or function which can help me
ASKER