vb.netcookieshttprequestrestsharpx-www-form-urlencoded

How to create Http request with header and body format x-www-form-urlencoded and get the cookies value in vb.NET


I'm trying to make an http request with the following attributes

  1. Header - contains a key 'Referer' with a set value
  2. Body format is x-www-form-urlencoded
  3. Body contains multiple keyes 'type', 'encpwd', 'user', 'pwd' & 'admsid'

In the response from the request, I need to obtain values in the cookies. I'm able to do it via Postman get the cookies, however I need to write a function/method to achieve this and return the cookies values which will be required to authenticate in future API calls.

From the internet, I found a sample code as below

Dim client As RestSharp.RestClient = New RestSharp.RestClient(inEndpoint)
client.Timeout = 10000
client.CookieContainer = New System.Net.CookieContainer

Dim request As RestSharp.RestRequest = New RestRequest(RestSharp.Method.Post)
request.AddHeader("Referer", strReferer)

Dim response As IRestResponse = client.Execute(request)
responsecode=response.StatusCode
responsecontent=response.Content

Dim cookies =client.CookieContainer.GetCookieHeader(response.ResponseUri)
cookiecontainer=cookies.ToString

Console.WriteLine("cookieJar "+ cookies.ToString)
Console.WriteLine("response Uri "+response.ResponseUri.ToString)

But when I'm trying this code in Visual Studio (I have installed RestSharp package already), I'm getting lots of error as below:

client.Timeout = 10000  -- Timeout is not a member of RestClient

client.CookieContainer = New System.Net.CookieContainer -- Property 'CookieContainer' is 'Read-Only'

Dim request As RestSharp.RestRequest = New RestRequest(RestSharp.Method.Post) -- Type 'RestRequest' is not defined

request.AddHeader("Referer", strReferer) -- 'AddHeader' is not a member of 'RestRequest'

Dim response As IRestResponse = client.Execute(request) -- Type 'IRestResponse' is not defined

I'm quite new to https request, please help me where I'm making mistake. Also I don't know/find how to add the body parameters with x-www-form-urlencoded format, everywhere it's mentioned in JSON, but this API service only accepts this format for Body.

Thanks for the help in advance.

I've tried the below code

Dim client As RestClient = New RestClient(endPoint)
Dim request As RestRequest = New RestRequest(Method.Post)
request.AddHeader("Referer", strReferer)
request.AddParameter("application/x-www-form-urlencoded", "type=" + strType + "&encpwd= " + strEncPwd + "&user=" + strUser + "&pwd=" + strPwd + "&admsid=" + stradmsID, ParameterType.RequestBody)
Dim response = client.Execute(request)

It generates exception "cannot send a content-body with this verb-type."

Please help


Solution

  • The below code ran successfully.

        Dim client = New RestClient(endPoint)
        Dim request = New RestRequest()
        request.Method = Method.Post
        request.AddHeader("Referer", strReferer)
        request.AddHeader("Content-Type", "application/x-www-form-urlencoded")
        request.AddParameter("type", strType)
        request.AddParameter("encpwd", strEncPwd)
        request.AddParameter("user", strUser)
        request.AddParameter("pwd", strPwd)
        request.AddParameter("admsid", stradmsID)
        Dim response = client.Execute(request)
        Dim cookies = client.CookieContainer.GetCookieHeader(response.ResponseUri)
        Console.WriteLine("cookieJar " + cookies.ToString)
        Console.WriteLine(response.Content)
    

    Hope it will help someone. Thanks