Link to home
Create AccountLog in
Avatar of lancerxe
lancerxe

asked on

Web Reference to Non-Web Service Hosts

Hello Experts,

I need help with getting started on how to leverage this problem.  I appreciate some assistance or to point me to some useful directions to solving this dilenma.

I am currently writing an asp.net 2.0/vb.net web site application and some of my data sources are remote.  Three of my "Hosts" were good enough to provide me with these SOAP-based Web Services which I can easily create web references with in my project except one, hence the problem.

This host's web solution is using UNIX, Tomcat as Web App Server, with java server pages.

The only thing I was trying to do is: make a request to whatever the page to request, pass in the UID/PWD then arg1 and arg2 to query his application and get a string result back:

Here is the procedure that he gave me  **note I have ommitted some information:

<his instructions>

(1) You'd make a query with WebRequest for https://[host uri]/[some folder]/[some folder]/processing.jsp?
givendln=[passedVal1]&givenssn=[passedVal2]&outputformat=simple

(2) The response will be a "302 Moved Temporarily" to a login.jsp page.

This response will set a cookie that you should keep. You don't have to follow this 302, but you can. A normal web browser here would get the login form, but you don't have to, because you can follow the next step directly:

(3) You'll POST to http://[their host uri]/j_security_check
The POST should have the cookie. You'll post two variables:
j_username -- your bankcars login username j_password -- your bankcars login password

(4) The response to this will be a "302 Moved Temporarily" back to to the processing page you originally requested. Follow this redirect.

(5) The redirect will take you back to the processing page automatically. The response to this will be the HTML page with all of the detailed report on the query.

The parameter "outputformat=simple" returns a basic HTML page. We can develop another response format more appropriate for your needs.

</end of his instructions>

As of current, I cannot or do not have enough to even reference anything with this method.  I am afraid I am only familiar with making web refernces with Web Services.  Don't even know how to proceed with this one.

Can anyone please help me out with some advise or point me to the right direction to get a start?

Thanks in advance for any help and insights.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of lancerxe
lancerxe

ASKER

Hello Bob,

Thanks a million for a prompt and detailed reply.

Please give me a few days and try this out.  This does look like a viable object to expand upon and tackle the task I have.  I will let you know in a few days of how this pans out.  Again thanks a lot for giving me a start.
When you create HttpWebRequest object, how do you assign variables to it ?

Also, how do you attach your captured cookie from HTTPResponse into HTTPRequest (HTTPRequest only has CookieContainer property)?  Is there some sort of conversion/cast that I have to do with my captured Cookie object so I can somehow "hook" it into my HTTPRequest?

Here is what my code looks thus far:

Public Shared Function GetPageText_BankCars_Version(ByVal url As String, ByVal postUrl As String) As String

        Dim htmlText As String = String.Empty

        Dim readerText As StreamReader
        Dim streamIP As Stream
        Dim responseIP As HttpWebResponse
        Dim responseCookie As Cookie

        Try

            ' Create a request to myIPaddress.com.
            Dim requestIP As HttpWebRequest = WebRequest.Create(url)

            ' Set time out to 10 seconds.
            requestIP.Timeout = 10000

            ' Get the first response from the server.
            responseIP = requestIP.GetResponse()

            ' From this First Response, supposedly will set a cookie, we should capture it!
            If responseIP.Cookies.Count > 1 Then
                responseCookie = responseIP.Cookies(0)
            End If

            ' Now that we captured the cookie BankCars sent, we post two variables against it:
            ' j_username -- our bankcars uid, j_password -- our bankcars pwd
            ' From this point, we repurpose our request object
            If Not IsNothing(requestIP) Then requestIP = Nothing
           
            'then we recreate our request object with our post URL
            requestIP = WebRequest.Create(postUrl)

            ' Set time out to 10 seconds again!!!
            requestIP.Timeout = 10000

            ' Set this request method to post
            requestIP.Method = "post".ToUpper

            'insert our captured cookie into this request
            'requestIP.CookieContainer = responseCookie  --> this is wrong!!!

            'I still have to post two variables to this request object

            ' Get the text stream.
            streamIP = responseIP.GetResponseStream()

            ' Create a reader to access the text stream.
            readerText = New StreamReader(streamIP)

            ' Get the text from the stream.
            htmlText = readerText.ReadToEnd()

        Finally

            If Not readerText Is Nothing Then
                readerText.Close()
            End If

            If Not streamIP Is Nothing Then
                streamIP.Close()
            End If

            If Not responseIP Is Nothing Then
                responseIP.Close()
            End If

        End Try

        Return htmlText

    End Function  'GetPageText
Are you talking about the query string variables?

   givendln=[passedVal1]&givenssn=[passedVal2]&outputformat=simple

If so, then those get passed in the URL that you specify for the GetPageText_BankCars_Version function.



To get cookies, you need to do something like this:

1) Create a cookie container for the request:
   requestIP.CookieContainer = New CookieContainer

2) Get the cookies from the response:

   For Each cookie As Cookie In responseIP.Cookies
   Next cookie

Bob
Hello again Bob,

No sir I was talking about this procedure here:

(3) You'll POST to http://[their host uri]/j_security_check
The POST should have the cookie. You'll post two variables:
j_username -- your bankcars login username j_password -- your bankcars login password

Thanks very much for the cookie handling - I will try to do that instead, might be more elegant way to handle it.
OK Bob!

I am on testing phases now.  I've been able to handle a "POST" HTTPWebRequest and append POST variables to it.  This is how it looks like now:

Try
            ' Create a request to myIPaddress.com.
            Dim requestIP As HttpWebRequest = WebRequest.Create(url)

            ' Set time out to 10 seconds.
            requestIP.Timeout = 10000

            requestIP.AllowAutoRedirect = True

            ' Get the first response from the server.
            Try
                responseIP = requestIP.GetResponse()
            Catch ex As Exception
                If responseIP.StatusCode = HttpStatusCode.Found Then
                    MessageBox.Show("We can continue with request, status code of 302")
                End If
            End Try

            ' Now that we captured the cookie BankCars sent, we post two variables against it:
            ' j_username -- our bankcars uid, j_password -- our bankcars pwd
            ' From this point, we repurpose our request object
            If Not IsNothing(requestIP) Then requestIP = Nothing
            'then we recreate our request object with our post URL
            requestIP = WebRequest.Create(postUrl)

            ' Set time out to 10 seconds again!!!
            requestIP.Timeout = 10000

            ' Allow it to redirect
            requestIP.AllowAutoRedirect = True

            ' Set this request method to post
            requestIP.Method = "POST"

            ' Create the string containing the two variables
            Dim lcPostData As String = "j_username=" & HttpUtility.UrlEncode("our uid")
            lcPostData += "&j_password=" & HttpUtility.UrlEncode("our pwd")

            ' now convert this string to bytes, set the content length of HTTPWebRequest the same as buffer length
            Dim lbPostBuffer() As Byte = System.Text.Encoding.GetEncoding(1252).GetBytes(lcPostData)
            requestIP.ContentLength = lbPostBuffer.Length

            ' Initialize a stream from the HTTPWebRequest
            Dim loPostData As Stream = requestIP.GetRequestStream
            loPostData.Write(lbPostBuffer, 0, lbPostBuffer.Length)
            ' Close the stream
            loPostData.Close()

            ' insert our captured cookie into this request
            ' From this First Response, supposedly will set a cookie, we should capture it!
            requestIP.CookieContainer = New CookieContainer
            For Each __cookie As Cookie In responseIP.Cookies
                requestIP.CookieContainer.Add(__cookie)
            Next

            ' Get the text stream.
            streamIP = responseIP.GetResponseStream()

            ' Create a reader to access the text stream.
            readerText = New StreamReader(streamIP)

            ' Get the text from the stream.
            htmlText = readerText.ReadToEnd()

            ' Now that we got our string that encapsulate the data we wanted, clean the HTTPWebRequest
            If Not IsNothing(requestIP) Then requestIP = Nothing

        Finally

            If Not readerText Is Nothing Then
                readerText.Close()
            End If

            If Not streamIP Is Nothing Then
                streamIP.Close()
            End If

            If Not responseIP Is Nothing Then
                responseIP.Close()
            End If

        End Try

Right now we are having issues with our Remote Host's request responses (they are timing out on their side).  Will let you know in few days how this pans out.  But I am optimistic thus far with this class.  Thanks!
Hey Bob!

Gave you credit for the answers of course.  Yours is the model that we followed and have gotten us to the right track.  There are quite a few variations from the original source you posted but we used your method as blueprint for this solution.  We are still having problems but its really on the Host side that we need to resolve.  Otherwise, this solution you posted we are confident that would work with what we are trying to do.

Again thanks for all your assists.