Login to Website from Desktop App

Login and download data from a web source

Page 1 of 1

3 Replies - 17873 Views - Last Post: 16 January 2008 - 08:01 AM Rate Topic: -----

#1 mitchell500  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-August 07

Login to Website from Desktop App

Post icon  Posted 19 August 2007 - 11:05 AM

I need to log in to a website, navigate to a following site, then download a binary file (Excel) all from a desktop application. I am confused as to which control or assembly to use. I'm writing this as a class so I don't have direct access to controls on a form. I can't seem to find the right object to use. Here's my code so far:

	   Dim request As HttpRequest
		Dim response As HttpResponse = Nothing
		Dim reader As StreamReader
		Dim data As StringBuilder
		Dim byteData() As Byte
		Dim postStream As Stream = Nothing
		Dim Address As Uri

			Address = New Uri(URL)

			request = DirectCast(WebRequest.Create(Address.ToString), HttpRequest)

		Catch ex As Exception

		End Try

I'm getting an error stating that webrequest can't be cast to an httprequest. This particular line come directly from sample code from Yahoo. I'm relatively new to .net (obviously) and don't want to go down the wrong road (use the wrong object). Since I may need to deal with cookies, do I need to use something like the old iNet control from COM? Hopefully, there is a .net solution.

Is This A Good Question/Topic? 0
  • +

Replies To: Login to Website from Desktop App

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Login to Website from Desktop App

Posted 19 August 2007 - 01:03 PM

Are you trying to log into an ASP.Net website, or what kind of site?
Was This Post Helpful? 0
  • +
  • -

#3 Chris33478  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 12-September 07

Re: Login to Website from Desktop App

Posted 12 September 2007 - 11:31 AM

View PostPsychoCoder, on 19 Aug, 2007 - 01:03 PM, said:

Are you trying to log into an ASP.Net website, or what kind of site?

I am also VERY new to .NET and am trying to write an app to interface with a ASP website.

I've got it to connect to the website, but I get a "does not accept cookies" error in the header.

Dim strUsername As String
Dim strPassword As String
' Create an instance of the WinHTTPRequest ActiveX object.
Dim WinHttpReq = New WinHttp.WinHttpRequest
' HttpRequest SetCredentials flags.

Private Sub Command1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Command1.Click
	' Assemble an HTTP Request.
	WinHttpReq.Open("GET", "http://website.iamconnecting.to", False)
	' Set credentials for server.
	strUsername = "myusername"
	strPassword = "*****"
	WinHttpReq.SetCredentials(strUsername, strPassword, HTTPREQUEST_SETCREDENTIALS_FOR_SERVER)

	' Send the HTTP Request.

	' Put status and content type into status text box.
	Text1.Text = WinHttpReq.GetAllResponseHeaders & "" & WinHttpReq.ResponseText
End Sub

So I think my next step would be to allow the app to accept cookies. Unfortunately I have yet to discover how to go about doing that.

Any help would be much appreciated. :D

Was This Post Helpful? 0
  • +
  • -

#4 jeff_hladik  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 16-January 08

Re: Login to Website from Desktop App

Posted 16 January 2008 - 08:01 AM

I am having a similar problem, though somewhat different code. No matter what I seem to try, the internal website in question that I am attempting to access always indicates that my "browser" does not accept cookies. I can even view the header which contains the cookie, but the site still refuses to believe I accept cookies. Is there a setting somewhere that I am not seeing that will tell it that, yes, I do accept cookies? Here is a sample of my code (it may not be exactly correct, as I have been through about 50 attempts to get it to work, but it demonstrates roughly what I am trying to do)

Imports System
Imports System.Diagnostics
Imports System.Web
Imports System.Data
Imports System.Math
Imports System.Net
Imports System.Text
Imports System.IO

Module Module1

	Sub Main()

		Dim urlString As String
		Dim request As HttpWebRequest
		Dim response As HttpWebResponse
		Dim responseStream As IO.StreamReader

		' Establish the request
		urlString = "http://myinternalsite"
		request = WebRequest.Create(urlString)

		' Set request properties
		request.Timeout = 10000
		request.AllowAutoRedirect = True
		request.MaximumAutomaticRedirections = 1000
		request.MaximumResponseHeadersLength = 10000
		request.UserAgent = "VB Browser"

		' Set up requirements to check for cookies
		request.CookieContainer = New CookieContainer()

		' Connect to login page to get cookie
		response = CType(request.GetResponse(), HttpWebResponse)
		Console.WriteLine("Content length is {0}", response.ContentLength)
		Console.WriteLine("Content type is {0}", response.ContentType)

		Console.WriteLine("Headers Coming Up")

		responseStream = New StreamReader(response.GetResponseStream(), Encoding.UTF8)
		Console.WriteLine("Response Stream Received.")

		' Adds a pause to wait for input

	End Sub

End Module

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1