LPARAMETERS m_IP, m_Port
SET TALK OFF
SET DATE ANSI
SET HOURS TO 24
SET CENTURY ON
LOCAL Call_Count
Call_Count = 0
SET LIBRARY TO VFPHttpServer.fll
&&连接到主进程
IF !HTTP_Connect(m_IP, VAL(m_Port))
RETURN
ENDIF
LOCAL Ev as Boolean
LOCAL SessionSetted as Boolean &&是否已设置了SESSION
SessionSetted = .F.
&&设置发送缓冲区大小
Http_SetChunkSize(1024)
Do While Http_IsConnected()
m.Ev = HTTP_GetEvent(5000) &&获得事件,有请求到达返回真,参数:整形,超时时间
IF (m.Ev)
*!* *!* *以下演示COOKIE操作
m.CookieValue = Http_GetCookie("TNND") &&获得指定名称的COOKIE的值
IF EMPTY(m.CookieValue)
Http_SetCookie("TNND", "NTNND") &&设置COOKIE必须在第一次Write之前进行
ENDIF
HTTP_Write("Hello World! " + TTOC(DATETIME()) + " 第" + TRANSFORM(Call_Count) + "次复用" +CHR(13) + CHR(13))
Call_Count = Call_Count + 1
*!* *演示获得URL各部分内容,分别有以下几部分:
*!* HUF_PATH = 3,
*!* HUF_QUERY = 4,
m.UrlPath = Http_GetUrlField(3)
Http_Write("URL访问路径为:" + m.UrlPath + CHR(13) + CHR(13))
*!*
*!* *以下演示头域值获取,Http_GetHeader()也可以直接传入头域字段名
m.oHeader = CREATEOBJECT("Empty")
Http_Write( "头域Host的值为:" + Http_GetHeader("Host") + CHR(13)) &&传入头域名,返回头域的值,没找到则返回空字符
m.HeaderItemSize = Http_GetHeader(-1) &&传入负值,返回头域总数
Http_Write("头域总数:" + TRANSFORM(m.HeaderItemSize) + CHR(13))
FOR i=0 to m.HeaderItemSize - 1
Http_GetHeader(i, m.oHeader) &&传入不大于总数的正值(从0开始)和一个接收结果的对象,对象自动添加key和value属性
Http_Write( 0h09 + m.oHeader.key + ":" + m.oHeader.value + CHR(13) ) &&对象中已设置好key和value属性
ENDFOR
*!* *!*
*!* *!*
*!* *以下演示查询参数的获取,Http_GetQueryField()函数里也可以直接传入参数名,没找到返回空字符串
m.QueryFieldsCount = Http_GetQueryField(-1) &&传入负数,获得查询参数的个数
m.oQueryField = CREATEOBJECT("Empty") &&创建一个“空”对象,用来接收键值对
Http_Write( CHR(13) + "查询参数[" + TRANSFORM(m.QueryFieldsCount) + "]:" + CHR(13))
FOR i=0 to m.QueryFieldsCount - 1
Http_GetQueryField(i, m.oQueryField) &&遍历查询参数,起始序号为0
Http_Write( 0h09 + m.oQueryField.key + ":" + m.oQueryField.value + CHR(13))
ENDFOR
*!* *!*
*!* *!* *以下演示POST数据的获取,Http_GetPostField()函数里也可以直接传入参数名,没找到返回空字符串
m.FormFieldsCount = Http_GetFormField(-1) &&传入负数,获得POST数据的个数
m.oFormField = CREATEOBJECT("Empty") &&创建一个“空”对象,用来接收键值对
Http_Write( CHR(13) + "POST数据[" + TRANSFORM(m.FormFieldsCount) + "]:" + CHR(13))
FOR i=0 to m.FormFieldsCount - 1
Http_GetFormField(i, m.oFormField) &&遍历POST数据,起始序号为0
Http_Write( 0h09 + STRCONV(m.oFormField.key, 11) + ":" + STRCONV(m.oFormField.value, 11) + CHR(13))
ENDFOR
m.RawPostData = Http_GetPostRawData()
Http_Write( CHR(9) + "原始数据:" + m.RawPostData + CHR(13))
*!* *!*
*!* *!* *以下演示SESSION操作
IF !m.SessionSetted
Http_SetSession("TMD", "TMD:" + TRANSFORM(DATETIME()), 900) &&设置SESSION,最后一个参数为超时时间,可省略,默认900秒
m.SessionSetted = .T.
ENDIF
m.SessionString = Http_GetSession("TMD") &&获得SESSION
*!* Http_DelSession("TMD") &&删除SESSION
Http_Write( CHR(13) + "SESSION TMD = " + m.SessionString + CHR(13) + CHR(13))
*!*
Http_Write( "COOKIE:" + CHR(13) + CHR(9) + "TNND:" + m.CookieValue + CHR(13))
*!* *!*
Http_Write("上传文件:" + 0h0D)
m.oFileInfo = CREATEOBJECT("Empty")
m.FileCount = Http_GetFileInfo(-1) &&传入负数,返回文件个数
FOR i=0 to m.FileCount - 1
IF Http_GetFileInfo(i, m.oFileInfo) = 0 &&正确获得数据返回 0
Http_Write( 0h09 + "上传文件名:" + m.oFileInfo.FileName + 0h09 + "文件大小:" + TRANSFORM(m.oFileInfo.FileSize) + " Bytes" + 0h09 + "表单名:" + m.oFileInfo.FieldName + 0h0d)
ENDIF
ENDFOR
Http_End() &&调用以后再写入均被忽略
ELSE &&空闲,可以干干其它事,相当于一个定时器,间隔时间由GetEvent的参数指定
ENDIF
ENDDO