[英]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我正在尝试使用以下属性发出 http 请求
In the response from the request, I need to obtain values in the cookies.在请求的响应中,我需要获取 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.
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:但是当我在 Visual Studio 中尝试这段代码时(我已经安装了 RestSharp package),我遇到了很多错误,如下所示:
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.我对 https 请求很陌生,请在我犯错误的地方帮助我。 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.
此外,我不知道/找到如何使用 x-www-form-urlencoded 格式添加正文参数,在 JSON 中提到的所有地方,但是此 API 服务仅接受此格式的正文。
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请帮忙
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
谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.