Results 1 to 3 of 3

Thread: RestSharp recommend usage

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2012
    Posts
    40

    RestSharp recommend usage

    Good afternoon forum,
    I have a project that uses RestSharp. When I make a request such as a POST I get a session cookie in return which is dropped into a Cookie Container. This all easy if I only use it on 1 form. How would I go bout having the cookie container present in all forms. I currently create a new client per request and close it when I'm done. Should I just create the Cookie Container as a global in my Mainform and pass it to the client everytime its created or is there a better use.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: RestSharp recommend usage

    There's nothing "global" about something declared in the main form. The whole point of "global" is that it can be accessed anywhere, which generally means in a module in VB. That said, if the main form is where you create all the clients then a member variable in the main form to store the cookie container is quite appropriate. I'd be inclined to create a class whose job it was to manage the cookie container and all the clients though.

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2012
    Posts
    40

    Re: RestSharp recommend usage

    This is my MainForm Code.

    Code:
    Public Class MainMenu
    
        Public Authenticated = False
        Public GlobCookieJar As CookieContainer
    
        Private Sub MainMenu_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            If Not Authenticated Then
                Login.ShowDialog()
            End If
        End Sub
    End Class
    This is my restsharp class:

    Code:
    Public Class MyApi
    
        Private Url As String = My.Settings.url
        Private _CookieJar As CookieContainer
        Private httpclient As RestClient
    
        Sub New(ByVal CookieJar As CookieContainer)
            _CookieJar = CookieJar
    
        End Sub
    
        Function Login(ByVal EmployeeId As String, ByVal PinNumber As String) As Boolean
            httpclient = New RestClient("http://127.0.0.1:3000")
            httpclient.CookieContainer = _CookieJar
            Dim request = New RestRequest("/login", Method.POST)
            request.RequestFormat = DataFormat.Json
            request.AddBody(New With {.empId = EmployeeId, .pin = PinNumber})
            Dim response As RestResponse = httpclient.Execute(request)
            If Not response.StatusCode = HttpStatusCode.OK Then
                Return False
            End If
            Return True
        End Function
    
    End Class
    Usage:

    Code:
    dim client = new MyApi(GlobCookieJar)
    client.Login("1234", "1234")
    The GlobCookieJar contains a session cookie which I pass to the server in order to authenticate the user. So I must keep it global so different forms can make the api calls and be authenticated. If there is an easier way please let me know. The reason I put it in the MainForm its becuase I know the mainform is available through whole program. Thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width