Question:
How do you make a program access the internet?
Akash J
2011-01-10 13:30:09 UTC
I'm a programmer, and I know Java, MATLAB, BASIC, a bit of C++ and a few other languages.

I was wondering, how would I write a program so that it can access the internet?

For example, how would I make a program go to Yahoo! Finance, so that I could get the specific value of a stock, or something like that.

Is there a way to program that, and/or can you give me the reference to a website that can teach me how to do that?

Thank you,
Akash Jain
Four answers:
2011-01-10 13:36:01 UTC
This is part of the code that I have written to access Yahoo!Finance. It is in Visual Basic 6.



Attribute VB_Name = "modInternet"

Option Explicit

Public Const IF_FROM_CACHE As Long = &H1000000

Public Const IF_MAKE_PERSISTENT As Long = &H2000000

Public Const IF_NO_CACHE_WRITE As Long = &H4000000

Public Const INTERNET_OPEN_TYPE_DIRECT As Long = 1

Const INTERNET_OPEN_TYPE_PRECONFIG = 0

Const INTERNET_FLAG_EXISTING_CONNECT = &H20000000



Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" ( _

ByVal sAgent As String, _

ByVal lAccessType As Long, _

ByVal sProxyName As String, _

ByVal sProxyBypass As String, _

ByVal lFlags As Long) As Long

Public Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" ( _

ByVal hInternetSession As Long, _

ByVal sURL As String, _

ByVal sHeaders As String, _

ByVal lHeadersLength As Long, _

ByVal lFlags As Long, _

ByVal lContext As Long) As Long

Public Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer

Public Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer

Public Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

Public txtFolder As String

Public Const sPrefix = "http://finance.yahoo.com/q?s="

Public myIni As New clsIniFile



Public Sub DownloadFile(URL As String, SaveAsFile As String)

Dim B

B = URLDownloadToFile(0, URL, SaveAsFile, 0, 0)

End Sub



Public Function rDownloadUrlSource(ByVal URL As String, l As Label) As String



On Error Resume Next

Const BUFFER_LEN As Long = 1024

Dim hInternet&, hFile&, lReturn&, sBuffer As String * BUFFER_LEN

Dim lCount As Long

XErr.LogThis "In Download"

On Local Error GoTo URLSourceError



If LCase(Left(URL, Len("http://"))) <> "http://" Then

URL = "http://" + URL

End If

hInternet = InternetOpen("vb wininet", INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, 0)

If hInternet = 0 Then

GoTo URLSourceError

End If



hFile = InternetOpenUrl(hInternet, URL, vbNullString, ByVal 0&, IF_NO_CACHE_WRITE, ByVal 0&)

If hFile = 0 Then

GoTo URLSourceError

End If

Call InternetReadFile(hFile, sBuffer, BUFFER_LEN, lReturn)

rDownloadUrlSource = Left(sBuffer, lReturn)

While Not lReturn = 0

Call InternetReadFile(hFile, sBuffer, BUFFER_LEN, lReturn)

rDownloadUrlSource = rDownloadUrlSource + Left(sBuffer, lReturn)

Wend

Call InternetCloseHandle(hInternet)

Exit Function



URLSourceError:

rDownloadUrlSource = vbNullString

If hInternet Then

Call InternetCloseHandle(hInternet)

End If

End Function



--------------------------------



I cannot give away all the code, because I have put in a lot of effort.
2011-01-10 13:55:48 UTC
I answered a similar question to this a few years ago. If you're willing to use .NET (available for free from microsoft - search for .NET express editions) there is a web control and you simply navigate to the url, then handle it's 'navigated' event and you can get the source of any page. It's almost too easy, but here's the code in VB.NET. It's nearly the same in C# so if you're already familiar with C++, that's probably a better fit for you:



______________________________________________________________________________



You declare a browser , navigate to the url, wait until the page is loaded and then check the documenttext property of the webbrowser object.



The easiest way is to use the webbrowser control from the controls collection



consider this code:



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Me.WebBrowser1.Navigate("http://www.goog…



End Sub



Private Sub WebBrowser1_Navigated(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigated… Handles WebBrowser1.Navigated

MessageBox.Show(Me.WebBrowser1.DocumentT…



End Sub







this code will navigate to the google website, then show the source when the page has completed loading



Once you have the source, which is nothing more than a string of text, you can search through it and parse it out for whatever you need from it.
2011-01-10 13:35:30 UTC
you would read the webpage source and use regular expressions to grab the numbers from the page.
Chris C
2011-01-10 14:44:45 UTC
Really very simple, if you're not dealing with SSL.



ifstream myStream( "http://finance.yahoo.com" );



if (myStream)

{

   while ( ! myStream.eof() )

   {

      // Do something here with the web page

   }



   // Close the stream

   myStream.close();

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...