How to log into router with HttpWebRequest? - vb.net

I'm trying to figure out how to successfully log into a DSL-Router (Model: Speedport w504v Type A).
I wrote a function usinig HttpWebRequest and HttpWebResponse. So far this function is not finished and is only for finding out the right login process.
Public Function DoRequest(ByVal url As String, ByVal password As String, ByVal container As CookieContainer) As String
'Login Request
Dim reqLogin As HttpWebRequest = DirectCast(HttpWebRequest.Create("https://speedport.ip/cgi-bin/login.cgi"), HttpWebRequest)
reqLogin.CookieContainer = container
reqLogin.Method = "POST"
reqLogin.Referer = "https://speedport.ip/hcti_start_passwort.stm"
reqLogin.KeepAlive = True
reqLogin.Host = "speedport.ip"
reqLogin.ContentType = "application/x-www-form-urlencoded"
reqLogin.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
reqLogin.Headers.Add("Accept-Language", "de-de,de;q=0.8,en-us;q=0.5,en;q=0.3")
reqLogin.Headers.Add("Accept-Encoding", "gzip, deflate")
'Login Data
Dim encoding As New ASCIIEncoding()
Dim data As String = Uri.EscapeDataString("pws") & "=" & Uri.EscapeDataString(password)
Dim bytes As Byte() = encoding.GetBytes(data)
reqLogin.ContentLength = bytes.Length
Dim stream As Stream = reqLogin.GetRequestStream
stream.Write(bytes, 0, bytes.Length)
stream.Close()
'Login Response
Dim resLogin As HttpWebResponse = DirectCast(reqLogin.GetResponse(), HttpWebResponse)
'Receive Cookie
Dim CookieHeaderValue As String = reqLogin.Headers.Get("Cookie")
If CookieHeaderValue <> Nothing Then
Dim aCookie As String() = CookieHeaderValue.Split("=")
Dim Cookie As New Cookie
Cookie.Domain = "speedport.ip"
Cookie.Path = "/"
Cookie.Secure = True
Cookie.Name = aCookie(0)
Cookie.Value = aCookie(1)
container.Add(Cookie)
End If
'Url Request
Dim reqIndex As HttpWebRequest = DirectCast(HttpWebRequest.Create(url), HttpWebRequest)
reqIndex.CookieContainer = container
reqIndex.Method = "GET"
reqIndex.Referer = "https://speedport.ip/wait_login.stm"
reqIndex.KeepAlive = True
reqIndex.Host = "speedport.ip"
reqIndex.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
'Url Content
Dim resIndex As HttpWebResponse = DirectCast(reqIndex.GetResponse(), HttpWebResponse)
Dim sr As StreamReader = New StreamReader(resIndex.GetResponseStream())
Dim output As String = sr.ReadToEnd
resIndex.Close()
Return output
End Function
Unfortunately I don't get the right content. Instead I'm getting the sitecontent from an Error-Page saying:
double administration access!
This site is returned when you try to login but there is another session already running.
So maybe I'm already successfully logged in but don't get the site content.
I get the header information from an Firefox AddOn called HTTP Live Header.
I also tried to run curl but also did not work:
curl -d "pws=PASSWORD" -c cookies.txt -e https://speedport.ip/hcti_start_passwort.stm -k https://speedport.ip/cgi-bin/login.cgi
curl -c cookies.txt -e https://speedport.ip/wait_login.stm -k https://speedport.ip/index.stm
Maybe someone has an idea what's going wrong.

Using Fiddler I figured it out. I've to set also the User Agent Header for the first request.
reqLogin.UserAgent = "YOUR USERAGNET STRING"
Furthermore I don't have to get the value for reqLogin.Headers.Get("Cookie").
It's already set to the CookieContainer so cancel the "Receive Cookie" part.

Related

Extract cookie from browser and attach to WebRequest

I have the following code to send a WebRequest to Betfair, usually this would be sent from a browser and it would send cookie data with it to identify my login, how would I go about attaching the cookie data to this request? (I need to find out how to extract the cookie data first, if anyone can point to any useful articles relating to that it would be much appreciated).
Private Sub Place_Bet(ByVal marketId As String, selectionId As String)
Dim url As Uri
url = New Uri("https://etx.betfair.com/www/etx-json-rpc?_ak=nzIFcwyWhrlwYMrh&alt=json")
Dim json As String = "[{""method"":""ExchangeTransactional/v1.0/place....}]"
Dim data = Encoding.UTF8.GetBytes(json)
Dim result_post = SendRequest(url, data, "application/json", "POST")
MsgBox(result_post)
End Sub
Private Function SendRequest(uri As Uri, jsonDataBytes As Byte(), contentType As String, method As String) As String
Dim response As String
Dim request As WebRequest
request = WebRequest.Create(uri)
request.ContentLength = jsonDataBytes.Length
request.ContentType = contentType
request.Method = method
Using requestStream = request.GetRequestStream
requestStream.Write(jsonDataBytes, 0, jsonDataBytes.Length)
requestStream.Close()
Using responseStream = request.GetResponse.GetResponseStream
Using reader As New StreamReader(responseStream)
response = reader.ReadToEnd()
End Using
End Using
End Using
Return response
End Function

error 'The given key was not present in the dictionary' when trying to API HTTP request token with Blue Prism

Im trying to use the Blue Prism object HTTP request to get an access token for further processing the items. However, i couldn't manage to get the token due to the error 'The given key was not present in the dictionary'. I have looked all parameters and still didn't manage to solve the issue. I use a built in visual basic code to get the result as a collection which is later parsed to JSON to get the token.
The underlying visual basic code is:
Dim request As WebRequest = WebRequest.Create(addressURL)
If forcePreAuth Then
'Sometimes a web server will require the authorisation header in the initial request
'In which case we have to add the basic authorization header manually.
Dim bytes() As Byte = System.Text.Encoding.UTF8.GetBytes(String.Format("{0}:{1}",username,password))
Dim base64 As String = Convert.ToBase64String(bytes)
request.Headers.Add("Authorization", "Basic " & base64)
Else
If Not String.IsNullOrEmpty(username) AndAlso Not String.IsNullOrEmpty(password) Then
request.Credentials = New NetworkCredential(username,password)
End If
End If
If useProxy Then
Dim proxyURI As New Uri(proxyURL)
Dim proxy As New WebProxy(proxyURI, True)
Dim proxyCred As New NetworkCredential(proxyUsername, proxyPassword)
Dim credCache As New CredentialCache()
credCache.Add(proxyURI, "Basic", proxyCred)
proxy.UseDefaultCredentials = False
proxy.Credentials = credCache
request.Proxy = proxy
End If
request.Method = method
request.ContentType = contentType
Dim httpRequest As HttpWebRequest = TryCast(request, HttpWebRequest)
If httpRequest IsNot Nothing Then
If Not String.IsNullOrEmpty(accept) Then
httpRequest.Accept = accept
End If
If Not String.IsNullOrEmpty(certID) Then
httpRequest.ClientCertificates.Add(m_Certificates(certID))
End If
End If
For Each r As DataRow In headers.Rows
For Each c As DataColumn In headers.Columns
Dim columnName As String = c.ColumnName
Dim val As String = r(columnName).ToString
request.Headers.Add(columnName,val)
Next
Exit For 'Only one row is allowed
Next
If Not String.IsNullOrEmpty(body) Then
Dim requestStream As IO.Stream = request.GetRequestStream()
Using sw As New IO.StreamWriter(requestStream, New Text.UTF8Encoding(False))
sw.Write(body)
End Using
End If
Using response As WebResponse = request.GetResponse()
Dim responseStream As IO.Stream = response.GetResponseStream()
Dim sr As New IO.StreamReader(responseStream)
resultData = sr.ReadToEnd()
End Using
Screenshots:
Input parameter
Input parameter request token
Request token:
Output parameter:
Blue Prism uses a bit of a peculiar pattern regarding Certificates. The way the Utility - HTTP object is designed is to allow for the loading of certificate files (.cer, etc) on-the-fly into a local Certificate Store, which assigns a new Certificate ID each time.
Before firing your HTTP Request, use the Load Certificate action within the same Utility - HTTP object to output a valid Certificate ID, which you can then pass to HTTP Request's Certificate ID parameter.

Translate Curl's --key and --cert to VB.NET

I need to post via VB.NET two files, a key file and a pem file, this can be done via Curl with the command:
curl https://someWebSite.com/test --key ./private_key.key --cert ./certificate.pem -d grant_type=password -d username="" -d password=""
So far I've done this in VB.NET but it just pops an error that says "Additional information: The remote server returned an error: (401) Unauthorized.
i reached one example where they use a *.p12 file and i tried to recreate it in VB.NET but still saying the same error. This is my Code:
Dim token As String = ""
Dim uriVar As New Uri("https://someWebSite.com/test")
Dim user As String = ""
Dim password As String = ""
Dim request As HttpWebRequest = CType(WebRequest.Create(uriVar), HttpWebRequest)
request.KeepAlive = False
request.ProtocolVersion = HttpVersion.Version11
request.Method = "POST"
Dim credential As New NetworkCredential(user, password)
request.Credentials = credential
Dim keystoreFileName As String = "keystore.p12"
Dim pathCert As String = Path.Combine(Environment.CurrentDirectory, "cert\", keystoreFileName)
Dim cert As New X509Certificate(pathCert, "allpassword")
request.ClientCertificates.Add(cert)
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = 39
request.Accept = "*/*"
Dim requestStream As Stream = request.GetRequestStream()
Dim post_data As String = "grant_type=password&username=&password="
Dim postBytes As Byte() = Encoding.ASCII.GetBytes(post_data)
requestStream.Write(postBytes, 0, postBytes.Length)
requestStream.Close()
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse) 'Here pops the error...
Dim responseJson = New StreamReader(response.GetResponseStream()).ReadToEnd()
Return responseJson

RestSharp response forbidden

I trying get a simple request to https://c-cex.com/t/prices.json
If i use this url in browser the correct response is showed , but if i make the same request using RestRequest i receive the 403 error Forbidden in all times. i trying HttpClient,WebRequest i get the same error
I put the header with user-agent, no cache and a lot of another values but did not work
Any ideas about this problem?
Dim url As String = "https://c-cex.com/t/prices.json"
Dim client As New RestSharp.RestClient(url)
Dim request = New RestRequest(url, Method.GET)
Dim response = client.Execute(request)
Updated code with apisign request
Dim nonce As String = CInt((DateTime.UtcNow - New DateTime(1970, 1, 1)).TotalSeconds).ToString
Dim url As String = "https://c-cex.com/t/api.html?a=getbalance&currency=BTC&apikey=" & API_KEY & "&nonce=" & nonce
Dim keybytes() = UnicodeEncoding.ASCII.GetBytes(API_SECRET)
Dim hs As System.Security.Cryptography.HMACSHA512 = New System.Security.Cryptography.HMACSHA512(keybytes)
Dim urlbytes() = UnicodeEncoding.ASCII.GetBytes(url)
Dim sh() = hs.ComputeHash(urlbytes)
Dim client As New RestSharp.RestClient(url)
Dim request = New RestRequest(url, Method.GET)
request.AddHeader("Cache-Control", "no-cache")
request.AddHeader("apisign", HttpUtility.UrlEncode(ToHexString(sh)))
Dim response = client.Execute(request)
I am getting the response without providing the header information and It looks given piece of code is working correctly for me in VB and C#.As a best practice, base URL needs to be specified in RestClient and relative URL in RestRequest.
Please recheck without adding the request header information
VB:
Imports RestSharp
Module Module1
Sub Main()
Dim host As String = "https://c-cex.com"
Dim endpoit As String = "t/prices.json"
Dim client As New RestSharp.RestClient(host)
Dim request = New RestRequest(endpoit, Method.GET)
Dim response = client.Execute(request)
Console.WriteLine("Response Body " + response.Content)
Console.WriteLine("Response Code " + response.StatusDescription)
End Sub
End Module
C#:
String host = "https://c-cex.com";
String endpoint = "t/prices.json";
RestClient _restClient = new RestClient(host);
var request = new RestRequest(endpoint, Method.GET);
var response = _restClient.Execute(request);
Console.WriteLine("Response Body :"+response.Content);
Console.WriteLine("Response Status Code :" + response.StatusDescription);

How to build valid PUT rest call that requires CSRF token

I get the "400" error when i do this PUT call (vb.net).
What am i doing wrong?
Dim wRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create("https://localhost:8444/api/route/1802/state"), HttpWebRequest)
wRequest.Method = "PUT"
wRequest.ContentType = "text/plain"
Dim stringData As String = "STOP"
Dim data = Encoding.ASCII.GetBytes(stringData)
wRequest.ContentLength = data.Length
Dim newStream = wRequest.GetRequestStream()
newStream.Write(data, 0, data.Length)
newStream.Close()
wResponse = DirectCast(wRequest.GetResponse(), HttpWebResponse)
The last line throw the 400.
this call doc
X-CSRF-Token was missing.
Do a GET and store the CSRF token from the response.
If(String.IsNullOrWhiteSpace(_csrfToken))
_csrfToken = wResponse.Headers("X-CSRF-Token")
End If
Add the token to PUT request before the "GetResponse"
If(not String.IsNullOrWhiteSpace(_csrfToken))
wRequest.Headers("X-CSRF-Token") = _csrfToken
End If
Note: X-CSRF-Token have the lifespan of the session.