Create cookie in HttpClient

时光毁灭记忆、已成空白 提交于 2020-01-06 05:48:10

问题


I am making calls to an api that works with "token" the login and the calls to the api work perfectly for me, I am using the class: "System.Net.Http.HttpClient".

Now I need to make a GET call but not to an API endpoints but to the site itself. Through the browser I enter the URL with login and it shows me a jSon object in the GET call, it is what I want to get in my .Net app.

[edit]

I do not need to write the code, what I need to know is: I am developing an application that connects to the API through "HttpClient" API authentication is by means of "token", so I login through of my app with POST. However, what I need a jSon file that is obtained in the browser with another URL that is not from the API, here the authentication is done with an on-screen form. If I take that URL to Postman it only works if I give it a "sessionid" in the header.

How can I connect to that URL from my app? Should I use a cookie? Should I get that jSon with another type of authentication?

This is the call I make in the browser to get the jSon: https://prnt.sc/okkai6

This is the call by Postman: https://prnt.sc/okivpg


回答1:


We're not supposed to write code for you, especially since you haven't provided anything. But here, you can have the code from my app.

    '/// = = = = = COOKIE GRAB
    <Runtime.InteropServices.DllImport("wininet.dll", SetLastError:=True)>
    Public Shared Function InternetGetCookieEx(ByVal url As String, ByVal cookieName As String, ByVal cookieData As StringBuilder, ByRef size As Integer, ByVal dwFlags As Int32, ByVal lpReserved As IntPtr) As Boolean
    End Function
    Private Const InternetCookieHttponly As Int32 = &H2000
    Public Shared Function GetUriCookieContainer(ByVal uri As Uri) As CookieContainer
        Dim cookies As CookieContainer = Nothing
        Dim datasize As Integer = 8192 * 16
        Dim cookieData As StringBuilder = New StringBuilder(datasize)

        If Not InternetGetCookieEx(uri.ToString(), Nothing, cookieData, datasize, InternetCookieHttponly, IntPtr.Zero) Then
            If datasize < 0 Then Return Nothing
            cookieData = New StringBuilder(datasize)
            If Not InternetGetCookieEx(uri.ToString(), Nothing, cookieData, datasize, InternetCookieHttponly, IntPtr.Zero) Then Return Nothing
        End If

        If cookieData.Length > 0 Then
            cookies = New CookieContainer()
            cookies.SetCookies(uri, cookieData.ToString().Replace(";"c, ","c))
        End If
        Return cookies
    End Function

    Private Cookies_Holder As New CookieContainer()
    Private Sub GetCookies(ByRef WhichCookie As CookieContainer)
        Dim WebUri As Uri = New Uri(WebBrowser1.Url.Scheme & "://" & WebBrowser1.Url.Host)
        WhichCookie = GetUriCookieContainer(WebUri)
    End Sub

Use GetCookies function at the place where you want to grab them (eg, after you log in and page is loaded).



来源:https://stackoverflow.com/questions/57192068/create-cookie-in-httpclient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!