0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

vbsの作法 その42

Last updated at Posted at 2019-10-20

概要

vbsの作法調べてみた。
winsock使ってみる。

環境

windows7 64bit

MSWinsock.Winsock

VB6のactivexなので、却下。

OSWINSCK.Winsock

配布されていたので、使ってみる。

サンプルコード

Option Explicit
Dim oWin
Dim sURL
Dim sPage
Dim sServer
Dim nPort
Dim sBuffer
Dim sSource
Dim bClose
Dim sProxy

Sub soc_OnClose()
	oWin.CloseWinsock
	WScript.Echo sSource
	bClose = True
	Set oWin = Nothing
End Sub

Sub soc_OnConnect()
	oWin.SendData "GET " & sPage & " HTTP/1.0" & vbCrLf & vbCrLf
End Sub

Sub soc_OnDataArrival(ByVal bytesTotal)
	oWin.GetData sBuffer
	sSource = sSource & sBuffer
End Sub

Sub soc_OnError(ByVal Number, Description, ByVal Scode, ByVal Source, ByVal HelpFile, ByVal HelpContext, CancelDisplay)
	WScript.Echo Number & ": " & Description
	oWin.CloseWinsock
End Sub

sURL = ""
sPage = ""
sServer = ""
nPort = 80
sBuffer = ""
sSource = ""
bClose = False
sProxy = ""
sURL = InputBox("Enter the URL", "OSWINSCK test", "http://www.ostrosoft.com")
sServer = Trim(sURL)
If InStr(sServer, "://") > 0 Then sServer = Mid(sServer, InStr(sServer, "://") + 3)
If InStr(sServer, "/") > 1 Then
	sPage = Mid(sServer, InStr(sServer, "/"))
	sServer = Left(sServer, InStr(sServer, "/") - 1)
	If InStr(sPage, "#") > 1 Then sPage = Left(sPage, InStr(sPage, "#") - 1)
Else
	sPage = "/"
End If
If InStr(sServer, ":") > 1 Then
	nPort = Mid(sServer, InStr(sServer, ":") + 1)
	sServer = Left(sServer, InStr(sServer, ":") - 1)
End If
If sServer <> "" Then
	Set oWin = CreateObject("OSWINSCK.Winsock")
	WScript.ConnectObject oWin, "soc_"
	If sProxy = "" Then
		oWin.Connect CStr(sServer), CLng(nPort)
	Else
		sPage = "http://" & sServer & sPage
		oWin.Connect CStr(sProxy), 80
	End If
Else
	sSource "Invalid URL"
	bClose = True
End If
While Not bClose
	WScript.Sleep 1
Wend





以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?