lotus agent request_content 如何分隔字段

lotus agent request_content how to separate fields

我有一个lotus代理运行lotusscript。形成浏览器我 post 形成数据到网络服务器,我使用以下 lotusscript 接收此数据:request_method = doc.GetItemValue( "request_content" )(0)

但是如果我有一个表格,例如姓名和电话号码。然后我的代理收到这个 name=bla&phonenumber=243525

我如何实际分离这些字段,其次我如何在此代理上接收 XML,以便我可以将其提取并放入文档中。我在谷歌上搜索了很多,但仍然没有找到解决方案。

如果客户端进行 GET 或 POST,您获取数据的方式会有所不同。 如果这是一个get,所有参数都在url中,格式为url。

网络上的许多资源将为您提供一些代码来解析此 url 并获取名称和值,在 goolge 中进行简单搜索将带来:http://searchdomino.techtarget.com/tip/Parsing-URL-Parameters-with-Ease

我通常使用以下代码,它在文档上下文中添加在 url 或 post 上收到的字段。

    Dim s As NotesSession
Set s = New notessession
Set doc = s.documentcontext
Dim myQuerystring As String

If doc Is Nothing Then
    logErrorEX "getting a call without document context ?!?","", doc,""
    GoTo returnErr
End If
If doc.QUERY_STRING_DECODED(0)<>"" Then'it's a GET
    myQuerystring = doc.QUERY_STRING_DECODED(0)
ElseIf doc.QUERY_STRING(0)<>"" Then
    myQuerystring = doc.QUERY_STRING(0)
    'decode it !
ElseIf doc.REQUEST_CONTENT(0)<>"" Then'it's a POST
    myQuerystring = doc.REQUEST_CONTENT(0) ' WARNING this is for POST but you will have to decode !!!
    'decode it !
Else
    logErrorEX "getting a call with document context but without query_string?!?","", doc,""
    GoTo returnErr
End if
Call ExplodeQueryString(myQuerystring, doc)

Private Sub  ExplodeQueryString (QueryString As String,doc As NotesDocument )

Dim ArgsList As Variant 

ArgsList = Split (QueryString,  "&")
If IsArray(ArgsList) Then 
    debugString = debugString+"ArgsList is an array of " & UBound(ArgsList) 
Else
    debugString = debugString+"ArgsList is NOT an array ??? " & ArgsList
End if
Dim ArgKey As String
Dim ArgValue As String
ForAll Arg In ArgsList
    If left$(Arg, 1)= "_" Or Left$(Arg, 1)= "%" Then
        'ignore it      
    else 
        ArgKey = strleft(Arg, "=")
        If ArgKey = "" Then
            'ignore it?
        else
            ArgValue = strright$(Arg, "=")
            '               AgentArgs(ArgKey) = ArgValue
            doc.Replaceitemvalue ArgKey, ArgValue
        End If
    End if
End ForAll
End Sub

我没有声明一些像 debugString 这样的全局变量来缩短。

您看到的格式是所有网络浏览器软件用来对表单中的字段数据进行编码的约定。您可以使用类似于 Emmanual 发布的代码中的 ExplodeQueryString 函数的函数来解析它。在我看来,他正在获取每个“&name”部分并创建一个具有该名称的 NotesItem,并使用它来存储“=value”部分的值。您可以这样做,也可以使用列表,或任何最适合您要求的方法。

没有规则禁止在不使用 &name=value 约定的情况下以其他格式发送 POST 数据。它只需要在发送方的任何软件和接收方的软件之间达成协议。如果他们想在 POST 数据中向您发送 XML,那很好。您可以使用标准 XML 解析函数来处理它。 Notes 附带一个 NotesDOMParsesr class,您可以根据需要使用它。如果您在 Windows 上 运行,则可以改用 Microsoft.XMLDOM。

我不久前写了一个 class 完全符合您的要求。它将查询字符串(或请求内容)拆分为一个值列表,名称作为列表标签。

http://blog.texasswede.com/free-code-class-to-read-url-name-value-pairs/

这是代码(我通常把它放在一个名为Class.URL的脚本库中):

%REM
    Library Class.URL
    Created Oct 9, 2014 by Karl-Henry Martinsson
    Description: Lotusscript class to handle incoming URL (GET/POST).
%END REM
Option Public
Option Declare

%REM
    Class URLData
    Description: Class to handle URL data passed to web agent
%END REM
Class URLData
    p_urldata List As String

    %REM
        Sub New()
        Description: Create new instance of URL object from NotesDocument 
    %END REM
    Public Sub New()
        Dim session As New NotesSession
        Dim webform As NotesDocument
        Dim tmp As String
        Dim tmparr As Variant  
        Dim tmparg As Variant
        Dim i As Integer

        '*** Get document context (in-memory NotesDocument)
        Set webform = session.DocumentContext
        '*** Get HTTP GET argument(s) after ?OpenAgent
        tmp = FullTrim(StrRight(webform.GetItemValue("Query_String")(0),"&"))
        If tmp = "" Then
            '*** Get HTTP POST argument(s) after ?OpenAgent
            tmp = FullTrim(StrRight(webform.GetItemValue("Request_Content")(0),"&"))    
        End If
        '*** Separate name-value pairs from each other into array
        tmparr = Split(tmp,"&")      
        '*** Loop through array, split each name-value/argument 
        For i = LBound(tmparr) To UBound(tmparr)
            tmparg = Split(tmparr(i),"=")
            p_urldata(LCase(tmparg(0))) = Decode(tmparg(1))
        Next
    End Sub

    %REM
        Function GetValue
        Description: Get value for specified argument.
        Returns a string containing the value.
    %END REM
    Public Function GetValue(argname As String) As String
        If IsElement(p_urldata(LCase(argname))) Then
            GetValue = p_urldata(LCase(argname))
        Else        
            GetValue = ""   
        End If
    End Function

    %REM
        Function IsValue
        Description: Check if specified argument was passed in URL or not.
        Returns boolean value (True or False).
    %END REM
    Public Function IsValue(argname As String) As Boolean
        If IsElement(p_urldata(LCase(argname))) Then
            IsValue = True
        Else        
            IsValue = False 
        End If
    End Function


    '*** Private function for this class
    '*** There is no good/complete URL decode function in Lotusscript
    Private Function Decode(txt As String) As String
        Dim tmp As Variant 
        Dim tmptxt As String
        tmptxt = Replace(txt,"+"," ")
        tmp = Evaluate(|@URLDecode("Domino";"| & tmptxt & |")|)
        Decode = tmp(0)
    End Function

End Class

这是您可以使用它的方式:

Option Public
Option Declare
Use "Class.URL"

Sub Initialize
    Dim url As URLData

    '*** Create new URLData object
    Set url = New URLData()

    '*** MIME Header to tell browser what kind of data we will return
    Print "content-type: text/html"

    '*** Check reqired values for this agent
    If url.IsValue("name")=False Then
        Print "Missing argument 'name'."
        Exit Sub
    End If

    '*** Process name argument
    If url.GetValue("name")="" Then
        Print "'Name' is empty."
    Else
        Print "Hello, " + url.GetValue("name") + "!"
    End If

End Sub