Topic: VB.NET HttpWebRequest Example
Greetings, I created this today and I know its kind of incomplete but i've gotten it to basically work for me. I wanted one that could do Chase for my company and the financial software im developing for them. I will post the source code free on here for others to examine and learn with. As far as I know this is the only example i've found for accessing OFX Direct connect in the .NET. For the "Account Activity" porition I copied the OFX spec directly from the OFX website in their banking example. I still cannot get the Account List request to work I get a 400 bad request any help on that would be appreciated. Without further ado here it is.
Public Class OFXDownload
Dim objReq As HttpWebRequest
Dim DataBuffer As New StringBuilder
Dim ReqStream As IO.Stream
Dim _ResponseString As String
Dim objResponse As HttpWebResponse
Dim ResponseStream As IO.Stream
Dim CurrGuid As String
Dim _UserName As String
Dim _Password As String
Public Enum RequestType
AccountList = 0
AccountActivity = 1
End Enum
Public Sub New(ByVal ofx_RequestType As RequestType, ByVal userName As String, ByVal password As String, ByVal AccountID As String, ByVal BankID As String, ByVal org As String, ByVal FID As String)
_UserName = userName
_Password = password
CurrGuid = Guid.NewGuid.ToString
Dim clientDate As String
clientDate = Format(DateTime.Now, "yyyyMMddHHmmss")
DataBuffer.AppendLine("OFXHEADER:100")
DataBuffer.AppendLine("DATA:OFXSGML")
DataBuffer.AppendLine("VERSION:102")
DataBuffer.AppendLine("SECURITY:NONE")
DataBuffer.AppendLine("ENCODING:USASCII")
DataBuffer.AppendLine("CHARSET:1252")
DataBuffer.AppendLine("COMPRESSION:NONE")
DataBuffer.AppendLine("OLDFILEUID:NONE")
DataBuffer.AppendLine("NEWFILEUID:NONE")
DataBuffer.AppendLine()
DataBuffer.AppendLine("<OFX>")
DataBuffer.AppendLine("<SIGNONMSGSRQV1>")
DataBuffer.AppendLine("<SONRQ>")
DataBuffer.AppendLine("<DTCLIENT>" & clientDate)
DataBuffer.AppendLine("<USERID>" & userName)
DataBuffer.AppendLine("<USERPASS>" & password)
DataBuffer.AppendLine("<LANGUAGE>ENG")
DataBuffer.AppendLine("<FI>")
DataBuffer.AppendLine("<ORG>" & org)
DataBuffer.AppendLine("<FID>" & FID)
DataBuffer.AppendLine("</FI>")
DataBuffer.AppendLine("<APPID>QWIN")
DataBuffer.AppendLine("<APPVER>0900")
DataBuffer.AppendLine("</SONRQ>")
DataBuffer.AppendLine("</SIGNONMSGSRQV1>")
Select Case ofx_RequestType
Case RequestType.AccountList
DataBuffer.AppendLine("<SIGNUPMSGSRQV1>")
DataBuffer.AppendLine("<ACCTINFOTRNRQ>")
DataBuffer.AppendLine("<ACCTINFORQ>")
DataBuffer.AppendLine("<DTACCTUP>19700101000000")
DataBuffer.AppendLine("</ACCTINFORQ>")
DataBuffer.AppendLine("</ACCTINFOTRNRQ>")
DataBuffer.AppendLine("</SIGNUPMSGSRQV1>")
DataBuffer.AppendLine("</OFX>")
Case RequestType.AccountActivity
DataBuffer.Append("<BANKMSGSRQV1>")
DataBuffer.Append("<STMTTRNRQ>")
DataBuffer.Append("<TRNUID>" & CurrGuid)
DataBuffer.Append("<STMTRQ>")
DataBuffer.Append("<BANKACCTFROM>")
DataBuffer.Append("<BANKID>" & BankID)
DataBuffer.Append("<ACCTID>" & AccountID)
DataBuffer.Append("<ACCTTYPE>CHECKING")
DataBuffer.Append("</BANKACCTFROM>")
DataBuffer.Append("<INCTRAN>")
DataBuffer.Append("<INCLUDE>Y")
DataBuffer.Append("</INCTRAN>")
DataBuffer.Append("</STMTRQ>")
DataBuffer.Append("</STMTTRNRQ>")
DataBuffer.Append("</BANKMSGSRQV1>")
DataBuffer.Append("</OFX>")
'later
End Select
End Sub
Public Function Download() As String
MsgBox(DataBuffer.ToString)
Dim byteBuffer() As Byte = Encoding.ASCII.GetBytes(DataBuffer.ToString)
Dim responseReader As StreamReader
objReq = WebRequest.Create("https://ofx.chase.com")
objReq.Method = "POST"
objReq.ContentType = "application/x-ofx"
objReq.Accept = "*/*, application/x-ofx"
objReq.ContentLength = byteBuffer.Length
System.Net.ServicePointManager.Expect100Continue = False
ReqStream = objReq.GetRequestStream
ReqStream.Write(byteBuffer, 0, byteBuffer.Length)
ReqStream.Close()
objResponse = objReq.GetResponse
ResponseStream = objReq.GetResponse.GetResponseStream
responseReader = New StreamReader(ResponseStream)
_ResponseString = responseReader.ReadToEnd
Return _ResponseString
End Function
Public ReadOnly Property ResponseString()
Get
ResponseString = _ResponseString
End Get
End Property
End Class
and an example of how to use:
Dim MyOFXData as new OFXDownload(OFXDownload.RequestType.AccountActivity,"username","password","ACCOUNTID","BANKID","ORG","FID")
MessageBox.show(MyOFXData.Download)
This will return in string format the OFX Data. Parsing it is of course up to you.