Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FileMaker + VB.NET, GET/POST, XML

Status
Not open for further replies.
Jul 10, 2008
32
0
0
US
Oh man, I really hope someone can help me.

I am an IS intern and I am very new to Visual Basic, very, VERY new. I guess you could say I'm self-teaching myself on the job, but I am taking a class next semester. Anyway, my company uses two FileMaker systems that integrate. When you click "Create ID" in the first app, it takes you to the second app, you fill out all the details there, hit "Assign ID", it saves the record and it then takes you back to the first app (where it populates that field with the ID you just created). So basically, the ID in the first app is like a foreign key, and to see all the details you have to go to the second app.

My company has contracted another company to develop a .NET solution to replace the first app. The only problem is that now, we'll need to integrate the .NET solution with the second app (which is FileMaker). My manager would like to develop a sort of "middle man" web service. When the user clicks the "Create ID" in the .NET app, the data will be sent to the web service as XML, the web service will parse the XML and translate it into FileMaker schema, and then send it on its way to the FileMaker app. The new FileMaker record will then be pre-populated with all the info that was sent from the .NET app. He said HTTP GET and POST will need to be utilized for this to work.

Now like I said, I'm new to all this and trying to learn it. I made a sample GET function just to practice with the concepts:

Code:
<WebMethod()> _
    Public Function GetWebPageHtml() As String
        Dim url As String
        Dim request As HttpWebRequest
        Dim response As HttpWebResponse
        Dim responseStream As StreamReader
        Dim htmlAsString As String

        url = "[URL unfurl="true"]http://www.google.com"[/URL]
        ' Establish the request 
        request = WebRequest.Create(url)
        request.Method = "GET"

        'Retrieve request info headers
        response = request.GetResponse()
        responseStream = New StreamReader(response.GetResponseStream())
        htmlAsString = responseStream.ReadToEnd()
        Return htmlAsString

        response.Close()
        responseStream.Close()
    End Function

And it retrieved the HTML of Google's main page. But I am unsure as to how to POST a new record to the web-enabled FileMaker DB. Right now I'm using a test database. I know I must use a query string something like this:
Code:
[URL unfurl="true"]http://localhost:8888/FMPro?-db=TABLENAME.fp5&-lay=web&-format=-fmp_xml&field1=value1&field2=value2&-new[/URL]

But for now, just to take things one step at a time, I've been trying to pass a query string that will just search the records and return the XML for them. But I cannot even get that to work so I must be doing something wrong. This is the query I'm using:
Code:
[URL unfurl="true"]http://localhost:8888/FMPro?-db=TABLENAME.fp5&-format=-dso_xml&field3=value3&-find[/URL]
When I type it directly into the browser it works, but I get a HTTP 500 error when I run the VB debugger. Here is the code I have for my sample function:
Code:
<WebMethod()> _
    Public Function Find()
        Dim request As HttpWebRequest
        Dim response As HttpWebResponse
        Dim path As String
        Dim responseStream As StreamReader
        Dim responseText As String
        
        path = "[URL unfurl="true"]http://localhost:8888/FMPro?-db=TABLENAME.fp5&-format=-dso_xml&field3=value3&-find"[/URL]
        request = WebRequest.Create(path)
        request.Method = "GET"

        response = request.GetResponse()
        responseStream = New StreamReader(response.GetResponseStream())
        responseText = responseStream.ReadToEnd()
        Return responseText
    End Function

There IS a password on the test DB, but I always log into the DB first in FileMaker Pro in order to open the port and enable web sharing. Do I still need to provide a password in my code, and if so, how? Use the credentials property of HttpWebRequest? When I paste the query string directly into the browser, it asks me for the password the first time but not susbequent times. Also, I believe in production the FileMaker database will always be logged in on the server (this would be necessary for the web companion to work, right?)

I would be eternally grateful if someone could shed some light on what I'm trying to learn/do, that is, tell me why my Find function isn't working and how to post a new record using a query string in VB. If I know how to post a new record in this hard-coded query string fashion, I can figure out how to factor in the XML part to pass parameters to the query string...but as it stands right now I can't get anything else working until I get this working.

Thanks!
 
Let me clarify...this is an ASP.NET web service.

I also posted this in the ASP.NET forum to see if anyone there can help.

I got my Find function working, and I am now able to run queries against the database using HTTP GET and the results are returned to me as XML. However, I am still having a problem with adding new records. I can add a new record with GET, but not POST. From my understanding, URIs have a length limit, and I've read that POST can be used in place of GET to avoid this limitation. I tried implementing a POST function based on sample code I saw, but I have a feeling I did it completely wrong. Like I said, I'm new to web services so forgive me.

This code works:
Code:
<WebMethod()> _
    Public Function Find() As String
        Dim uri As New Uri("[URL unfurl="true"]http://localhost:8888/FMPro?-db=TABLENAME.fp5&-format=-dso_xml&field1=value1&-find")[/URL]
        Dim credentials As NetworkCredential

        If (uri.Scheme = uri.UriSchemeHttp) Then
            Dim request As HttpWebRequest = HttpWebRequest.Create(uri)
            credentials = New NetworkCredential("", "password")
            request.Credentials = credentials
            request.Method = WebRequestMethods.Http.Get
            Dim response As HttpWebResponse = request.GetResponse()
            Dim reader As New StreamReader(response.GetResponseStream())
            Dim xml As String = reader.ReadToEnd()
            response.Close()
            Return xml
        End If
    End Function

And this code works:
Code:
<WebMethod()> _
<WebMethod()> _
    Public Function AddRecordWithHTTPGet() As String
        Dim uri As New Uri("[URL unfurl="true"]http://localhost:8888/FMPro?-db=TABLENAME.fp5&-format=-fmp_xml&field2=value2&-new")[/URL]

        Dim credentials As NetworkCredential

        If (uri.Scheme = uri.UriSchemeHttp) Then
            Dim request As HttpWebRequest = HttpWebRequest.Create(uri)
            credentials = New NetworkCredential("", "password")
            request.Credentials = credentials
            request.Method = WebRequestMethods.Http.Get
            Dim response As HttpWebResponse = request.GetResponse()
            Dim reader As New StreamReader(response.GetResponseStream())
            Dim xml As String = reader.ReadToEnd()
            response.Close()
            Return xml
        End If
    End Function

This code DOESN'T work:
Code:
<WebMethod()> _
    Public Function AddRecordWithHTTPPost()
        ' I get a "Format file not found" error:
        Dim uri As New Uri("[URL unfurl="true"]http://localhost:8888/FMPro?-db=TABLENAME.fp5&-format=-fmp_xml&-lay=webdown")[/URL]
        Dim param1 As String = "field3=value3&-new"

        Dim credentials As NetworkCredential

        If uri.Scheme = uri.UriSchemeHttp Then
            Dim request As HttpWebRequest = HttpWebRequest.Create(uri)
            credentials = New NetworkCredential("", "password")
            request.Credentials = credentials
            request.Method = WebRequestMethods.Http.Post
            request.ContentLength = param1.Length
            request.ContentType = "application/x-[URL unfurl="true"]www-form-urlencoded"[/URL]

            Dim writer As New StreamWriter(request.GetRequestStream)
            writer.Write(param1)
            writer.Close()

            Dim oResponse As HttpWebResponse = request.GetResponse()
            Dim reader As New StreamReader(oResponse.GetResponseStream())
            Dim xml As String = reader.ReadToEnd()
            oResponse.Close()
            Return xml
        End If

    End Function

I think I read somewhere that adding FileMaker records can only be done with GET. Yet, when I view the web-enabled database in the browser and click "New record", a blank form comes up and when I left-click and select "View source code", FORM METHOD=POST is in the code. So there must be a way to do it in ASP.NET. Does anyone know how?
 
Nevermind, I figured out how to perform the POST. :) The problem was with the URI. I did not have it formatted correctly...I didn't know the URI can only contain up to the question mark, and everything else must be contained in the POST body. I also found that even though my FM database has only a password (no username), I had to include a dummy username in the credentials, otherwise I could not authenticate. As long as I had at least one character in place of "username" it would work.

Thought I'd share in case anyone else ever runs into this problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top