我的工作任务是让我们公司的软件自动登录网页,这样我们就可以发送GET请求没有webbrowser或VS形式
我公司的软件在工业机器上运行,所以必须从Visual Studio中自动运行。 到目前为止,这个主题的所有答案都涉及某种浏览器,这就是我想问你们的原因:
我如何获取cookie以及如何使用它?
如何将UN和PW(代码方式)提交到网页的j_security部分并使用哪些参数?
网站以login.jsp或j_security.jsp结尾。我们被告知首先发送一个关于cookie的GET请求,然后通过POST发送用户名和PW。
我已经在2周前开始使用VB编程,并感谢任何帮助! :)
答案 0 :(得分:0)
您好请关注此网址。它会告诉你自动登录过程
您可能需要更改UserName和Password的元素名称。
转到“登录”页面 - >查看页面来源 - >转到用户名并将其替换为" txtUsername"在代码中。密码的过程相同。
希望这会有所帮助。
Nishit
答案 1 :(得分:0)
更新:
我设法解决了这些问题:
使用UN和PW向j_security_check发送POST请求(使用之前的cookie!)
Public Sub GetCookie()
Console.WriteLine("GlobalCookieContainer.Count {0}",GlobalCookieContainer.Count)
Dim URL As String = "https://DaWebsite.com/sthg/index.jsp?enable=all"
Dim request As HttpWebRequest = HttpWebRequest.Create(URL) 'Create a request for the URL.
request.CookieContainer = New CookieContainer() 'CookieContainer aufsetzen
GlobalCookieContainer = request.CookieContainer 'Cookies aus dem Request in den globalen CookieContainer laden
Dim response As HttpWebResponse = request.GetResponse() 'Get the response.
Console.WriteLine("resp.StatusCode ist: {0}, resp.StatusDescription ist: {1}", Int(response.StatusCode), response.StatusDescription)
Console.WriteLine("Server {0}, Uri {1}, Method {2}, Cookies {3}", response.Server, response.ResponseUri, response.Method, response.Cookies)
'DATA STREAM
Dim dataStream As Stream = response.GetResponseStream() 'Get the stream containing content returned by the server.
Dim reader As New StreamReader(dataStream) 'Open the stream using a StreamReader for easy access.
Dim responseFromServer As String = reader.ReadToEnd() 'Read the content.
'Console.WriteLine(responseFromServer.Substring(0,100)) 'Display the content.
reader.Close() 'Clean up the streams and the response.
response.Close()
Console.WriteLine("CookieContainer.Count: {0}",request.CookieContainer.Count) 'Anzahl der Cookies angeben
Console.WriteLine()
Console.ReadKey() Call JSecurity()
End Sub
Private Sub JSecurity() 'WebRequestPostExample.JSecurity()
Try
Console.WriteLine("JSecurity Anfrage")
Console.ReadKey()
Dim username As String = "UN" 'Username
Dim password As String = "PW" 'Password
Dim URL As String = "https://DaWebsite.com/stgh/j_security_check"
Dim request As HttpWebRequest = CType(WebRequest.Create(URL), HttpWebRequest)
request.CookieContainer = GlobalCookieContainer 'Cookie wiederverwenden
request.Method = "POST" 'Set the Method property of the request to POST.
Dim postData As String = "j_username=" & username & "&j_password=" & password 'Create POST data and convert it to a byte array.
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
request.ContentType = "application/x-www-form-urlencoded" ' Set the ContentType property of the WebRequest.
request.ContentLength = byteArray.Length ' Set the ContentLength property of the WebRequest.
'DATA STREAM
Dim dataStream As Stream = request.GetRequestStream() ' Get the request stream.
dataStream.Write(byteArray, 0, byteArray.Length) ' Write the data to the request stream.
dataStream.Close() ' Close the Stream object.
'RESPONSE
Dim resp As HttpWebResponse = request.GetResponse() ' Get the response.
Console.WriteLine("resp.StatusCode ist: {0}, resp.StatusDescription ist: {1}", Int(resp.StatusCode), resp.StatusDescription)
Console.WriteLine("Server {0}, Uri {1}, Method {2}, Cookies {3}", resp.Server, resp.ResponseUri, resp.Method, resp.Cookies)
Console.ReadKey() ' Display elements of repsonse that came from the request issued above.
dataStream = resp.GetResponseStream() ' Get the stream containing content returned by the server.
Dim reader As New StreamReader(dataStream) ' Open the stream using a StreamReader for easy access.
Dim responseFromServer As String = reader.ReadToEnd() ' Read the content.
'Console.WriteLine(responseFromServer) ' Display the content.
reader.Close() ' Clean up the streams.
dataStream.Close()
resp.Close()
Console.WriteLine()
If Int(resp.StatusCode) = 200 Then
LoggedIn = True
Else
LoggedIn = False
End If
Call TestConnection()
Catch e As WebException
Console.WriteLine("FEHLER")
Console.WriteLine(e.Message)
If e.Status = WebExceptionStatus.ProtocolError Then
Console.WriteLine("Status Code : {0}", CType(e.Response, HttpWebResponse).StatusCode)
Console.WriteLine("Status Description : {0}", CType(e.Response, HttpWebResponse).StatusDescription)
Console.WriteLine("Resonse URI: {0}", e.Response.ResponseUri)
Console.WriteLine("IsFromCache: {0}", e.Response.IsFromCache)
Console.WriteLine("IsMutuallyAuthenticated: {0}", e.Response.IsMutuallyAuthenticated)
Console.WriteLine()
Call StartLogin
End If
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End Sub