Retrieving username and password from Dropbox file - vb.net

I have a login tool which grabs the username and password from a file in my dropbox account.
Dim prequest As HttpWebRequest = HttpWebRequest.Create("http://dl.dropbox.com/s/jb4m5udkt96a43z/premuim.txt")
Dim presponse As HttpWebResponse = prequest.GetResponse()
Dim psr As IO.StreamReader = New IO.StreamReader(presponse.GetResponseStream())
Dim premium As String = psr.ReadToEnd()
That's the code I am using, it downloads the file and gives me the text inside. So I used this about half a year ago and it worked fine, now I am trying to use it but it doesn't work anymore.

Related

Download source of URL as string

I want my code to download the source/html of a website and be stored as a string.
For example
Dim source as string = html of website
This should work
Dim thesource As String = New System.Net.WebClient().DownloadString("http://the site u wanna look#")

Web application working on iis7 but not on local machine

I have my VB.NET web application successfully built and compiled on ISS7 server but on the local machine when compiling through Visual studion 2013 it is
returning "System.Security.Cryptography.CryptographicException: The system cannot find the file specified." error.
Not sure what is happening but throws an error at Dim cert As New X509Certificate2(certFile, certFilePassword,X509KeyStorageFlags.MachineKeySet)
Below is the code where I am trying to use a certificate that is installed on my local machine.
Sub LoginED(ByVal token As String)
Try
'We need the ED user ID
lblEDUserID.Text = GetEDUserID().ToString()
'Track user login
InsertLogin(txtEDUsername.Text, True)
'Set login cookies for ED users
If lblEDUserID.Text = "0" Then
lblEDLoginStatus.Text = "You have entered an incorrect login. Please try again."
Else
Dim certFile As String = "VIP_Cert.pfx"
Dim certFilePassword As String = "password"
Dim cert As New X509Certificate2(certFile, certFilePassword,X509KeyStorageFlags.MachineKeySet)
Dim vipSoapInterfaceQueryServiceCert As New WebReference.QueryService
'Dim fs As FileStream = File.Open(certFile, FileMode.Open, FileAccess.ReadWrite)
'Dim filesize As Long = fs.Length
'Dim buffer(filesize) As Byte
'fs.Read(buffer, 0, filesize)
'byte[] buffer = new byte[fs.Length];
'int count = fs.Read(buffer, 0, buffer.Length);
'fs.Close()
lblEDLoginStatus.Text = "Activation "
vipSoapInterfaceQueryServiceCert.ClientCertificates.Add(cert)
Dim vipSoapInterfaceService As New WebReference.GetCredentialInfoRequestType()
vipSoapInterfaceService.credentialId = "SMR23324543"
vipSoapInterfaceService.credentialType = WebReference.CredentialTypeEnum.STANDARD_OTP
vipSoapInterfaceService.requestId = "ABCD"
Dim vipSoapInterfaceResponse As New WebReference.GetCredentialInfoResponseType()
vipSoapInterfaceResponse = vipSoapInterfaceQueryServiceCert.getCredentialInfo(vipSoapInterfaceService)
lblEDLoginStatus.Text = "WebService Response: " + vipSoapInterfaceResponse.credentialId + ", Status:" + vipSoapInterfaceResponse.credentialStatus
End If
Finally
What I have tried so far:
1) Tried to specify the complete path of where the certificate file is on the machine.
2) Tried different formats of certificates (.pfx; .p12; .cer)
3) Tried the certificate open, read and sent into a buffer to take the input from a buffer.
(I removed the buffer logic when deploying on IIS7 as I placed the .pfx certificate file in System32 folder and the code could automatically recognise the certificate. But the local would not recognize)
4) I tried removing the "X509KeyStorageFlags.MachineKeySet" parameter. (I use that for the IIS7 compilation)
Any help is appreciated!!
Thanks! I could figure out what needed.
I had to create an App Pool for the Certificate and then provide Full Control permissions to that pool

VB.net loading out of date webpages

I use the following code to copy a certain line of text from the website listed.
'Track A
Dim TrackA As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("https://pilotweb.nas.faa.gov/common/nat.html")
Dim GaA As System.Net.HttpWebResponse = TrackA.GetResponse
Dim WrA As System.IO.StreamReader = New System.IO.StreamReader(GaA.GetResponseStream)
Dim ContentStrA As String = WrA.ReadToEnd
Dim StartIndexA As Integer = ContentStrA.IndexOf(vbLf & "A ") + 1
Dim StrLengthA As Integer = ContentStrA.IndexOf(vbLf, StartIndexA) - StartIndexA
However when I open the link in a tab in VB, it shows a version of the site from a few days ago, how do I prevent this from happening as I need the current version of the site?
Change the Cache Policy of your System.Net.HttpWebRequest object, so that you're not getting cached results.
Alternatively, you can clear the cache for your URL.

WebClient.UploadData "The underlying connection was closed"

I'm trying to upload a file from an FTP site to Basecamp using the Basecamp API. I'm using a simple console application. Here's my code:
Try
Dim accountID As String = ConfigurationManager.AppSettings("BaseCampID")
Dim projectID As Integer = 9999999
Dim folderName As String = "XXXXX/XXXXX"
Dim fileName As String = "XXX.zip"
'The URL to access the attachment method of the API
Dim apiURL = String.Format("https://basecamp.com/{0}/api/v1/projects/{1}/attachments.json", accountID, projectID)
'Get the file from the FTP server as a byte array
Dim fileBytes As Byte() = GetFileBytes(String.Format("{0}\\{1}", folderName, fileName))
'Initialize the WebClient object
Dim client As New WebClient()
client.Headers.Add("Content-Type", "application/zip")
'Need to provide a user-agent with a URL or email address
client.Headers.Add("User-Agent", "Basecamp Upload (email#email.com)")
'Keep the connection alive so it doesn't close
client.Headers.Add("Keep-Alive", "true")
'Provide the Basecamp credentials
client.Credentials = New NetworkCredential("username", "password")
'Upload the file as a byte array to the API, and get the response
Dim responseStr As Byte() = client.UploadData(apiURL, "POST", fileBytes)
'Convert the JSON response to a BaseCampAttachment object
Dim attachment As BaseCampAttachment
attachment = JSonHelper.FromJSon(Of BaseCampAttachment)(Encoding.Default.GetString(responseStr))
Catch ex As Exception
Console.WriteLine(ex.Message)
Finally
Console.ReadLine()
End Try
But whenever it calls client.UploadData, I get the error message "The underlying connection was closed: The connection was closed unexpectedly." I ran into this issue earlier and thought I solved it by adding the "Keep-Alive" header, but it's not working anymore. The API works if I upload a local file with client.UploadFile, but I'd like to just upload the file from they byte array from the FTP rather than downloading the file locally then uploading it to Basecamp.
Any thoughts would be greatly appreciated. Thanks!
I never figured out what was wrong with the WebClient call, but I ended up using a Basecamp API wrapper from https://basecampwrapper.codeplex.com. That wrapper uses HTTPRequest and HTTPResponse instead of WebClient.UploadData. It's also much easier to just use that wrapper than to try writing my own code from scratch.

Empty response HTTPWebRequest: Windows Phone 8

I am making a Windows Phone app and I am trying to get JSON data from a URL. The user needs to be logged into the website (which hosts the JSON data) in order to get JSON data and I cannot use the Web Browser control to display the data and then extract the string since the browser doesn't recognize it (for some weird reason) and asks to search for an app on Store which can handle that JSON file type. (If I open the URL in desktop browser on my Windows PC, I can see the raw JSON data). I can't use normal HTTPWebRequest or WebClient as to get JSON data the login cookies needs to be set (the JSON data is user-specific) and I can't extract the cookies from Web browser control and use it with WebClient or HTTPWebRequest. So the best thing I can do is use a special internal instance of IWebRequestCreate that is used internally by the WebBrowser. By opening background HTTP requests with that class, the cookies get automatically set as if they were created/sent by the WebBrowser control. But my code is not returning the JSON data, I get blank response, as in the string resp is empty.
Below is the code:
Dim browser = New WebBrowser()
Dim brwhttp = GetType(WebRequestCreator).GetProperty("BrowserHttp")
Dim requestFactory = TryCast(brwhttp.GetValue(Browser, Nothing), IWebRequestCreate)
Dim uri = New Uri("http://api.quora.com/api/logged_in_user?fields=inbox,notifs,following,followers")
Dim req = requestFactory.Create(uri)
req.Method = "GET"
req.BeginGetResponse(New AsyncCallback(AddressOf request_Callback), req)
Private Sub request_Callback(asyncResult As IAsyncResult)
Dim webRequest As HttpWebRequest = DirectCast(asyncResult.AsyncState, HttpWebRequest)
Dim webResponse As HttpWebResponse = DirectCast(webRequest.EndGetResponse(asyncResult), HttpWebResponse)
Dim tempStream As New MemoryStream()
webResponse.GetResponseStream().CopyTo(tempStream)
Dim sr As New StreamReader(tempStream)
Dim resp As String = sr.ReadToEnd
End Sub
What's wrong?
I found that CopyTo can leave the Stream's pointer at the end of the buffer, you probably need to reset tempStream's pointer to the beginning before attempting to read it with the StreamReader, here's the code...
webResponse.GetResponseStream().CopyTo(tempStream);
tempStream.Seek(0, SeekOrigin.Begin);
Dim sr As New StreamReader(tempStream);