Return Variables from Function

PutterPlace

Active member
Joined
Feb 18, 2008
Messages
37
Programming Experience
1-3
I have written a custom HTTP class that utilizes the HTTPWebRequest and HTTPWebResponse classes. The class will either "GET" or "POST" data to a specified URL via the "Send" function declared within my "HTTPClass".

Declaration of "Send" function:
VB.NET:
Expand Collapse Copy
Public Shared Function Send(ByVal URL As String, _
        Optional ByVal PostData As String = "", _
        Optional ByVal Method As HTTPMethod = HTTPMethod.HTTP_GET, _
        Optional ByVal ContentType As String = "", _
        Optional ByVal CookieJar As CookieContainer = Nothing)

This function has the ability to use a CookieContainer that is passed to the function. However, sometimes after posting information to certain URLs, the CookieCollection that was passed to the function has changed due to new cookies being set/unset. As of right now, this function only returns the html response of the HTTPWebRequest in the form of a string. Is there a way that I could return the response string and also the new CookieCollection, named "CookieJar" in an array of some sort?
 
Right now your function doesn't return anything.

Yes you can, you can set up a structure for all of the data you want to return from the function and simply return that.
 
Well..that wasn't the entire function. It was just to show how I declared the function. So I implemented a structure into a seperate class named Functions as follows:

Functions Class:
VB.NET:
Expand Collapse Copy
Imports System.Net

Public Class Functions

    Public Structure HTTPInfo
        Dim CookieJar As CookieContainer
        Dim ResponseData As String
    End Structure

EndClass

New HTTPClass:
VB.NET:
Expand Collapse Copy
Imports System.IO
Imports System.Net

Public Class HTTPClass
    Public Enum HTTPMethod As Short
        HTTP_GET = 0
        HTTP_POST = 1
    End Enum

    Public Shared Function Send(ByVal URL As String, _
        Optional ByVal PostData As String = "", _
        Optional ByVal Method As HTTPMethod = HTTPMethod.HTTP_GET, _
        Optional ByVal ContentType As String = "", _
        Optional ByVal CookieJar As CookieContainer = Nothing)

        Dim Request As HttpWebRequest = WebRequest.Create(URL)
        Dim Response As HttpWebResponse
        Dim SW As StreamWriter = Nothing
        Dim SR As StreamReader = Nothing
        Dim ResponseData As String
        Dim UserAgent As String = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1 (.NET CLR 3.5.30729)"

        Request.ServicePoint.Expect100Continue = False
        Request.Method = Method.ToString().Substring(5)
        Request.UserAgent = UserAgent
        If CookieJar.Count > 0 Then
            Request.CookieContainer = CookieJar
        Else
            Request.CookieContainer = New CookieContainer()
        End If

        If (Method = HTTPMethod.HTTP_POST AndAlso PostData <> "" AndAlso ContentType = "") Then
            ContentType = "application/x-www-form-urlencoded"
        End If

        If (ContentType <> "") Then
            Request.ContentType = ContentType
            Request.ContentLength = PostData.Length
        End If

        If (Method = HTTPMethod.HTTP_POST) Then
            Try
                SW = New StreamWriter(Request.GetRequestStream())
                SW.Write(PostData)
            Catch Ex As Exception
                Throw Ex
            Finally
                SW.Close()
            End Try
        End If

        Try
            Response = Request.GetResponse()
            SR = New StreamReader(Response.GetResponseStream())
            ResponseData = SR.ReadToEnd()
        Catch Wex As System.Net.WebException
            SR = New StreamReader(Wex.Response.GetResponseStream())
            ResponseData = SR.ReadToEnd()
            Throw New Exception(ResponseData)
            MsgBox(ResponseData)
        Finally
            SR.Close()
        End Try

        Dim ReturnData As Functions.HTTPInfo
        ReturnData.ResponseData = ResponseData
        ReturnData.CookieJar = CookieJar

        Return ReturnData
    End Function
End Class



Did I set that up correctly? Also, since I was only using the class to return the html response, I declared a variable named ResponseData in my frmMain class as follows:

VB.NET:
Expand Collapse Copy
Dim responseData As String = HTTPClass.Send("http://www.someURL.com/", "variable=value&variable2=value2", HTTPClass.HTTPMethod.HTTP_POST)

Since the Send function only returned a string, that was fine. However, If I'm returning structured data, would I need to change that to this:

VB.NET:
Expand Collapse Copy
Dim responseData as Functions.HTTPInfo = HTTPClass.Send("http://www.someURL.com/", "variable=value&variable2=value2", HTTPClass.HTTPMethod.HTTP_POST)

...then get the value for each variable using responseData.ResponseData and responseData.CookieJar ?
 
Last edited:
I just prefer to use the HttpWebRequest class since I'm more familiar with it. You misunderstood what I'm talking about though. I didn't actually write a custom HTTP class....I wrote a class containing a function that utilizes the HttpWebRequest and HttpWebResponse classes using data that is passed to it.

Anyways, I tried setting up the structure as I stated in my previous post, and it didn't seem to work correctly. The html response was successfully added into the "ResponseData" variable, but the CookieCollection was not. Have I done something wrong when declaring the CookieJar variable in the structure, or maybe I did something wrong when setting the value of CookieJar?
 
I know you didnt; i was just trying to say that microsoft have a class that can be used in place of yours, and will remember the cookie jar
 
I have done that in the past. However, now I am using multiple threads that will need to have a different CookieCollection in each thread. Also, I wanted to truncate my code a little bit, so I wanted to put the HttpWebRequest handling into a seperare function to which the threads can pass the information needed, and still use the information later since a function won't remember a CookieCollection....afaik.
 
I'm not using one HttpWebRequest with multiple threads. Multiple threads will be accessing a function which creates a new HttpWebRequest each time the function is called. This is the reason why I needed the ability to return a CookieCollection from the function as well as send one to it.

Could someone tell me what I did wrong when setting up/using that structure?
 
You really need a utility class to objectify/represent a web browser. It will store a URL, a cookie jar etc.. Once created, you can pass it around and maintain some form of state, that way youre not pushing cookies in and out of things all the time.

You'll write it and then realise that the original advice I gave you is still most valid: MS already wrote a utlity class to represent something like a web browser. It's WebClient (or shdocvw web browser :) )
 
From what I've read, I can't set a timeout for the WebClient class. Anyways...I decided to do away with the structure so that I don't need to pass the CookieCollection back and forth. Instead I setup a shared CookieCollection which seems to be working just fine. Thank you for all of your help. :)
 
Back
Top