Cita de: rigorvzla en 29 Mayo 2019, 18:36 PM
quiero que en la pagina de login ya sea: facebook, elhacker, hotmail etc... (cualquier web con login)
pueda ingresar los datos (usuario y clave) para que al abrir el navegador solo dirija a la pagina luego del login osea : bandeja de entrada, pagina de inicio de facebook o lo que siga luego del login.
Entendi lo que me explicastes inicialmente elektro de q el usuario no debe ver si no es necesario lo q se hace, solamente se debe mostrar el resultado, en este caso lo siguiente a la pagina de login.
Imagino que con alguno de esos ejemplos ya eres capaz de logearte en la página deseada y obtener la cookie?.
Luego, tan solo tienes que llamar a la función de Windows InternetSetCookieEx para "registrar" dicha cookie de manera temporal durante el tiempo de vida de tu programa (o de manera persistente también puedes, según quieras), y por último llamar al método WebBrowser.Navigate para mostrar la página post-login.
Cualquier conversor de VB.NET a C# debería ser capaz de traducir el siguiente código sin problemas...
Código (vbnet) [Seleccionar]
public class DevWebBrowser : inherits webbrowser
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Navigates to the specified url.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <param name="url">
''' The url to navigate.
''' </param>
'''
''' <param name="newWindow">
''' Indicates whether the url should be open into a new browser window.
''' </param>
'''
''' <param name="cookies">
''' The cookies to set for the specified url.
''' </param>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
<EditorBrowsable(EditorBrowsableState.Always)>
Public Overloads Sub Navigate(ByVal url As String, ByVal newWindow As Boolean, ByVal cookies As CookieCollection)
WebUtil.SetCookies(url, False, cookies)
MyBase.Navigate(url, newWindow)
End Sub
end class
+
Código (vbnet) [Seleccionar]
public notinheritable class WebUtil
private sub new()
end sub
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Sets one or more cookies for the specified URL.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <param name="url">
''' The URL.
''' </param>
'''
''' <param name="persistent">
''' A value that indicates the cookie persistance.
''' <para></para>
''' If set to <see langword="True"/> (persistent), the cookie is set in the operating system cache.
''' <para></para>
''' If set to <see langword="False"/> (non-persistent), the cookie is only set for the life-time of the current process.
''' </param>
'''
''' <param name="cookies">
''' The cookies to set.
''' </param>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Public Shared Sub SetCookies(ByVal url As String, ByVal persistent As Boolean, ByVal cookies As CookieCollection)
For Each cookie As Cookie In cookies
If (persistent) Then
Dim cookieData As String = $"{cookie.ToString()}; expires = {cookie.Expires.ToUniversalTime().ToString("ddd, dd-MMM-yyyy HH:mm:ss", CultureInfo.InvariantCulture.DateTimeFormat)} GMT"
If (NativeMethods.InternetSetCookieEx(url, Nothing, cookieData, 0, IntPtr.Zero) = 0) Then
Throw New Win32Exception(Marshal.GetLastWin32Error())
End If
Else
If (NativeMethods.InternetSetCookieEx(url, cookie.Name, cookie.Value, 0, IntPtr.Zero) = 0) Then
Throw New Win32Exception(Marshal.GetLastWin32Error())
End If
End If
Next cookie
End Sub
end class
+
Código (vbnet) [Seleccionar]
friend notinheritable class NativeMethods
private sub new()
end sub
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Creates a cookie with a specified name that is associated with a specified URL.
''' <para></para>
''' This function differs from the <see cref="NativeMethods.InternetSetCookie"/> function by being able to create third-party cookies
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <remarks>
''' <see href="https://docs.microsoft.com/en-us/windows/desktop/api/wininet/nf-wininet-internetsetcookiea"/>
''' </remarks>
''' ----------------------------------------------------------------------------------------------------
''' <param name="urlName">
''' The URL for which the cookie should be set.
''' </param>
'''
''' <param name="cookieName">
''' The name to be associated with the cookie data. If this parameter is <see langword="Nothing"/>, no name is associated with the cookie.
''' </param>
'''
''' <param name="cookieData">
''' Actual data to be associated with the URL.
''' </param>
'''
''' <param name="flags">
''' Flags that control how the function retrieves cookie data:.
''' </param>
'''
''' <param name="reserved">
''' <see cref="IntPtr.Zero"/>, or contains a pointer to a Platform-for-Privacy-Protection (P3P) header to be associated with the cookie.
''' </param>
''' ----------------------------------------------------------------------------------------------------
''' <returns>
''' Returns a member of the InternetCookieState enumeration if successful, or zero (0) otherwise.
''' </returns>
''' ----------------------------------------------------------------------------------------------------
<DllImport("WinINET.dll", SetLastError:=True, CharSet:=CharSet.Auto, BestFitMapping:=False, ThrowOnUnmappableChar:=True)>
friend Shared Function InternetSetCookieEx(ByVal urlName As String,
ByVal cookieName As String,
ByVal cookieData As String,
ByVal flags As Integer,
ByVal reserved As IntPtr
) As <MarshalAs(UnmanagedType.I4)> Integer
End Function
end class
Ejemplo de como usar todo eso:
Código (vbnet) [Seleccionar]
'This is a code example that demonstrates how to login to a website,
'and navigate to the page using the cookie that contains the login data.
Dim uri As New Uri("https://foro.elhacker.net/index.php?action=login2", UriKind.Absolute)
Dim cred As New NetworkCredential("USERNAME", "PASSWORD")
Dim query As String = HttpUtility.ParseQueryString($"cookielength=90&user={cred.UserName}&passwrd={cred.Password}").ToString()
Using client As New DevWebClient() With {.CookiesEnabled = True}
client.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded")
Dim response As String = client.UploadString(uri, "POST", query)
Console.WriteLine(response)
Dim cookies As CookieCollection = client.CookieContainer.GetCookies(uri)
DevWebBrowser1.Navigate("https://foro.elhacker.net", cookies)
End Using
...creo que no me faltó por compartir ningún miembro. La clase DevWebClient la compartí en un comentario de la primera página.
EDITO:
He visto muy de reojo los ejemplos que has puesto con la url de foro.elhacker.net, y los nombres de los parámetros están mal. Se llaman "user" y "passwrd" (sin la "o") tal y como puse en mis ejemplos. No "username", ni "usuario", ni "pass" ni "password" ni "clave" ni... en fin. No se hasta que punto entiendes lo que estás haciendo mal.
Saludos.