Java to .Net

I am trying to convert this code from Java to .net (C# or VB.Net) Any help would be appreciated.

Thanks!
public class ofx
{
	String url = "";
	String brokerId = "";
	String accountId = "";
	char[] password;
	String userId = "";
	String org = "";
	String fid = "777";
	
	public void setUrl(String newUrl)
	{
		url = newUrl;
	}
	
	public void setBrokerId(String newBrokerId)
	{
		brokerId = newBrokerId;
	}

	public void setOrg(String newOrg)
	{
		org = newOrg;
	}

	public void setFid(String newFid)
	{
		fid = newFid;
	}
	
	public void setInstitution(Institution i)
	{
		url = i.getUrl();
		brokerId = i.getBrokerId();
		org = i.getOrg();
		fid = i.getFid();
		accountId = i.getAccountId();
		password = i.getPassword();
		userId = i.getUserId();
	}

	private String generateOfxHeader()
	{
		return "OFXHEADER:100\nDATA:OFXSGML\nVERSION:102\nSECURITY:NONE\nENCODING:USASCII\nCHARSET:1252\nCOMPRESSION:NONE\nOLDFILEUID:NONE\nNEWFILEUID:0F7418F4-27DD-4ED1-968E-60D792642CA9\n\n<OFX>\n";
	}
	
	private String generateDate()
	{
		// Format today's date
		// TODO: check timezone...
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss.000");
		// TODO: is [-8:PST] necessary?
		return format.format(new Date());
	}
	
	private String generateInvestRequest()
	{		
		String ret = "<INVSTMTMSGSRQV1>\n<INVSTMTTRNRQ>\n<TRNUID>8CCCCD65-13AF-4464-8990-5A0E108ACA3E\n<CLTCOOKIE>4\n<INVSTMTRQ>\n<INVACCTFROM>\n<BROKERID>" + brokerId + "\n";
		ret += "<ACCTID>" + accountId + "\n";
		ret += "</INVACCTFROM>\n<INCTRAN>\n<DTSTART>19000101\n<INCLUDE>Y\n</INCTRAN>\n<INCOO>Y\n<INCPOS>\n<DTASOF>" + generateDate() + "\n";
		ret += "<INCLUDE>Y\n</INCPOS>\n<INCBAL>Y\n</INVSTMTRQ>\n</INVSTMTTRNRQ>\n</INVSTMTMSGSRQV1>\n";

		return ret;
	}
	
	private String generateAccountRequest()
	{
		return "<SIGNUPMSGSRQV1>\n<ACCTINFOTRNRQ>\n<TRNUID>C0A84BC5-6332-4674-ACEF-6149F15423B5\n<CLTCOOKIE>4\n<ACCTINFORQ>\n<DTACCTUP>19700101000000\n</ACCTINFORQ>\n</ACCTINFOTRNRQ>\n</SIGNUPMSGSRQV1>\n";
	}
	
	private String generateOfxFooter()
	{
		return "</OFX>";
	}
	
	private String generateSignon()
	{
		String ret;
		
		String now = generateDate();
		
		ret = "<SIGNONMSGSRQV1>\n<SONRQ>\n<DTCLIENT>" + now + "\n";
		ret += "<USERID>" + userId + "\n";
		ret += "<USERPASS>" + new String(password) + "\n";
		ret += "<LANGUAGE>ENG\n";
		if (org != "")
		{
			ret += "<FI>\n<ORG>" + org + "\n";
			if (fid != "")
			{
				ret += "<FID>" + fid + "\n";
			}
			ret += "</FI>\n";
		}
		//ret += "<APPID>QWIN\n<APPVER>1500\n</SONRQ>\n</SIGNONMSGSRQV1>\n";
		ret += "<APPID>Money\n<APPVER>1600\n</SONRQ>\n</SIGNONMSGSRQV1>\n";
		
		return ret;
	}
	
	private String queryServer(String query) throws MalformedURLException, UnsupportedEncodingException, IOException
	{
		//System.out.println("Query server");

		URL ofxUrl;
		
		//System.out.println(url);

		int port = 443; // default https port
		//port = 12345;
		ofxUrl = new URL(url);

		query = query.replaceAll("\n", "\r\n");
		//query = URLEncoder.encode(query, "UTF-8");
		//System.out.println("---- QUERY ----\n" + query + "\n---------------");

		// Write POST data
		OutputStreamWriter wr;
		BufferedReader in;
		String out = "";
		String ofxOut = "";
		try
		{
			SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
			
			SSLSocket socket = (SSLSocket) factory.createSocket(ofxUrl.getHost(), port);
			//Socket socket = new Socket(ofxUrl.getHost(), port);
			socket.setSoTimeout(5000);
			
			//System.out.println("Created socket");

			// Enable all the suites
			String[] supported = socket.getSupportedCipherSuites();
			socket.setEnabledCipherSuites(supported);
			
			Writer output = new OutputStreamWriter(socket.getOutputStream());
			output.write("POST " + url + " HTTP/1.1\r\n");
			output.write("Host: " + ofxUrl.getHost() + "\r\n");
			output.write("Content-type: application/x-ofx\r\n");
			output.write("Accept: */*, application/x-ofx\r\n");
			output.write("Content-Length: " + Integer.toString(query.length()) + "\r\n");
			output.write("\r\n");
			output.write(query);
			output.flush();

			// Get result
			in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
	
			String line;
			line = in.readLine();
			//System.out.println("Read: " + line);
			Boolean inOfx = false;
			while (line != null)
			{
				out += line + "\n";
				
				// Check for OFXHEADER
				if (!inOfx && line.length() >= 9 && line.substring(0, 9).equals("OFXHEADER"))
				{
					inOfx = true;
				}
				if (inOfx)
				{
					ofxOut += line + "\n";
				}
				line = in.readLine();
				//System.out.println("Read: " + line);
			}

			// Return read ofx
			return ofxOut;
		}
		catch (SocketTimeoutException e)
		{
			// Some sort of error, return all text
			return out;
		}
	}
	
	private String getAccount()
	{
		// First send an OFX query to get account id
		String query = generateOfxHeader() + generateSignon() + generateAccountRequest() + generateOfxFooter();
		String result;
		try
		{
			result = queryServer(query);
		}
		catch (Exception e)
		{
			// Nothing for now
			return "";
		}

		// Next regex to find acctid
		try
		{
			Pattern p = Pattern.compile("<ACCTID>\\s*([0-9]*)\\s*<");
			Matcher m = p.matcher(result);
			if (m.find() && m.groupCount() > 0)
			{
				//System.out.println("Got account #" + m.group(1));
				return m.group(1);
			}
		}
		catch (Exception e)
		{
			// nothing for now
			System.out.println("exception" + e);
		}
		
		return "";
	}
	
	void CheckOfx(String ofx) throws Exception
	{
		//throw new Exception("test");
		
		// Check for error
		Pattern p = Pattern.compile("<SEVERITY>\\s*ERROR\\s*<MESSAGE>\\s*([^<]*)\\s*");
		Matcher m = p.matcher(ofx);
		if (m.find() && m.groupCount() > 0)
		{
			throw new Exception(m.group(1));
		}

		p = Pattern.compile("<CODE>([^<]*)<SEVERITY>\\s*ERROR");
		m = p.matcher(ofx);
		if (m.find() && m.groupCount() > 0)
		{
			if (m.group(1).equals("2003"))
			{
				throw new Exception("Invalid account");
			}
			else
			{
				throw new Exception(m.group(1));
			}
		}
	}
	
	public String read() throws Exception
	{
		// If no account given, try getting account number.  If no account try
		// using userId
		if (accountId.length() == 0)
		{
			accountId = getAccount();
			if (accountId.length() == 0)
			{
				accountId = userId;
			}
		}
		//System.out.println("accountid: " + accountId);
		
		String query = generateOfxHeader() + generateSignon() + generateInvestRequest() + generateOfxFooter();
		String result = queryServer(query);
		CheckOfx(result);
		return result;
	}
	
	public void finalize()
	{
		Arrays.fill(password, '0');
	}
}

Open in new window

LVL 1
ndonhauserAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Roshan DavisCommented:
Java to C#.Net
public class ofx
{
	string url = string.Empty;
	string brokerId = string.Empty;
	string accountId = string.Empty;
	char[] password;
	string userId = string.Empty;
	string org = string.Empty;
	string fid = "777";

	public void setUrl(string newUrl)
	{
		url = newUrl;
	}

	public void setBrokerId(string newBrokerId)
	{
		brokerId = newBrokerId;
	}

	public void setOrg(string newOrg)
	{
		org = newOrg;
	}

	public void setFid(string newFid)
	{
		fid = newFid;
	}

	public void setInstitution(Institution i)
	{
		url = i.getUrl();
		brokerId = i.getBrokerId();
		org = i.getOrg();
		fid = i.getFid();
		accountId = i.getAccountId();
		password = i.getPassword();
		userId = i.getUserId();
	}

	private string generateOfxHeader()
	{
		return "OFXHEADER:100\nDATA:OFXSGML\nVERSION:102\nSECURITY:NONE\nENCODING:USASCII\nCHARSET:1252\nCOMPRESSION:NONE\nOLDFILEUID:NONE\nNEWFILEUID:0F7418F4-27DD-4ED1-968E-60D792642CA9\n\n<OFX>\n";
	}

	private string generateDate()
	{
		// Format today's date
		// TODO: check timezone...
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss.000");
		// TODO: is [-8:PST] necessary?
		return format.format(new Date());
	}

	private string generateInvestRequest()
	{		
		string ret = "<INVSTMTMSGSRQV1>\n<INVSTMTTRNRQ>\n<TRNUID>8CCCCD65-13AF-4464-8990-5A0E108ACA3E\n<CLTCOOKIE>4\n<INVSTMTRQ>\n<INVACCTFROM>\n<BROKERID>" + brokerId + "\n";
		ret += "<ACCTID>" + accountId + "\n";
		ret += "</INVACCTFROM>\n<INCTRAN>\n<DTSTART>19000101\n<INCLUDE>Y\n</INCTRAN>\n<INCOO>Y\n<INCPOS>\n<DTASOF>" + generateDate() + "\n";
		ret += "<INCLUDE>Y\n</INCPOS>\n<INCBAL>Y\n</INVSTMTRQ>\n</INVSTMTTRNRQ>\n</INVSTMTMSGSRQV1>\n";

		return ret;
	}

	private string generateAccountRequest()
	{
		return "<SIGNUPMSGSRQV1>\n<ACCTINFOTRNRQ>\n<TRNUID>C0A84BC5-6332-4674-ACEF-6149F15423B5\n<CLTCOOKIE>4\n<ACCTINFORQ>\n<DTACCTUP>19700101000000\n</ACCTINFORQ>\n</ACCTINFOTRNRQ>\n</SIGNUPMSGSRQV1>\n";
	}

	private string generateOfxFooter()
	{
		return "</OFX>";
	}

	private string generateSignon()
	{
		string ret;
		
		string now = generateDate();
		
		ret = "<SIGNONMSGSRQV1>\n<SONRQ>\n<DTCLIENT>" + now + "\n";
		ret += "<USERID>" + userId + "\n";
		ret += "<USERPASS>" + new string(password) + "\n";
		ret += "<LANGUAGE>ENG\n";
		if (org != string.Empty)
		{
			ret += "<FI>\n<ORG>" + org + "\n";
			if (fid != string.Empty)
			{
				ret += "<FID>" + fid + "\n";
			}
			ret += "</FI>\n";
		}
		//ret += "<APPID>QWIN\n<APPVER>1500\n</SONRQ>\n</SIGNONMSGSRQV1>\n";
		ret += "<APPID>Money\n<APPVER>1600\n</SONRQ>\n</SIGNONMSGSRQV1>\n";
		
		return ret;
	}

	private string queryServer(string query)
	{
		//Console.WriteLine("Query server");

		URL ofxUrl;
		
		//Console.WriteLine(url);

		int port = 443; // default https port
		//port = 12345;
		ofxUrl = new URL(url);

		query = query.replaceAll("\n", "\r\n");
		//query = URLEncoder.encode(query, "UTF-8");
		//Console.WriteLine("---- QUERY ----\n" + query + "\n---------------");

		// Write POST data
		OutputStreamWriter wr;
		BufferedReader buffin;
		string strout = string.Empty;
		string ofxOut = string.Empty;
		try
		{
			SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
			
			SSLSocket socket = (SSLSocket) factory.createSocket(ofxUrl.getHost(), port);
			//Socket socket = new Socket(ofxUrl.getHost(), port);
			socket.setSoTimeout(5000);
			
			//Console.WriteLine("Created socket");

			// Enable all the suites
			string[] supported = socket.getSupportedCipherSuites();
			socket.setEnabledCipherSuites(supported);
			
			Writer output = new OutputStreamWriter(socket.getOutputStream());
			output.write("POST " + url + " HTTP/1.1\r\n");
			output.write("Host: " + ofxUrl.getHost() + "\r\n");
			output.write("Content-type: application/x-ofx\r\n");
			output.write("Accept: */*, application/x-ofx\r\n");
			output.write("Content-Length: " + Integer.toString(query.length()) + "\r\n");
			output.write("\r\n");
			output.write(query);
			output.flush();

			// Get result
			buffin = new BufferedReader(new InputStreamReader(socket.getInputStream()));

			string line;
			line = buffin.readLine();
			//Console.WriteLine("Read: " + line);
			Boolean inOfx = false;
			while (line != null)
			{
				strout += line + "\n";
				
				// Check for OFXHEADER
				if (!inOfx && line.length() >= 9 && line.substring(0, 9).equals("OFXHEADER"))
				{
					inOfx = true;
				}
				if (inOfx)
				{
					ofxOut += line + "\n";
				}
				line = buffin.readLine();
				//Console.WriteLine("Read: " + line);
			}

			// Return read ofx
			return ofxOut;
		}
		catch (SocketTimeoutException e)
		{
			// Some sort of error, return all text
			return strout;
		}
	}
	
	private string getAccount()
	{
		// First send an OFX query to get account id
		string query = generateOfxHeader() + generateSignon() + generateAccountRequest() + generateOfxFooter();
		string result;
		try
		{
			result = queryServer(query);
		}
		catch (Exception e)
		{
			// Nothing for now
			return string.Empty;
		}

		// Next regex to find acctid
		try
		{
			Pattern p = Pattern.compile("<ACCTID>\\s*([0-9]*)\\s*<");
			Matcher m = p.matcher(result);
			if (m.find() && m.groupCount() > 0)
			{
				//Console.WriteLine("Got account #" + m.group(1));
				return m.group(1);
			}
		}
		catch (Exception e)
		{
			// nothing for now
			Console.WriteLine("exception" + e);
		}
		
		return string.Empty;
	}
	
	void CheckOfx(string ofx)
	{
		//throw new Exception("test");
		
		// Check for error
		Pattern p = Pattern.compile("<SEVERITY>\\s*ERROR\\s*<MESSAGE>\\s*([^<]*)\\s*");
		Matcher m = p.matcher(ofx);
		if (m.find() && m.groupCount() > 0)
		{
			throw new Exception(m.group(1));
		}

		p = Pattern.compile("<CODE>([^<]*)<SEVERITY>\\s*ERROR");
		m = p.matcher(ofx);
		if (m.find() && m.groupCount() > 0)
		{
			if (m.group(1).equals("2003"))
			{
				throw new Exception("Invalid account");
			}
			else
			{
				throw new Exception(m.group(1));
			}
		}
	}
	
	public string read()
	{
		// If no account given, try getting account number.  If no account try
		// using userId
		if (accountId.length() == 0)
		{
			accountId = getAccount();
			if (accountId.length() == 0)
			{
				accountId = userId;
			}
		}
		//Console.WriteLine("accountid: " + accountId);
		
		string query = generateOfxHeader() + generateSignon() + generateInvestRequest() + generateOfxFooter();
		string result = queryServer(query);
		CheckOfx(result);
		return result;
	}
	
	public void finalize()
	{
		Arrays.fill(password, '0');
	}
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ndonhauserAuthor Commented:
Thank you!
0
Roshan DavisCommented:
thanks, I am not sure whether it is fully completed. some of the updated functions are below.

Also, instead of the URL class, you may need to use WebClient. (http://bytes.com/topic/asp-net/answers/321066-url-class-c)
private string generateDate()
{
	// Format today's date
	// TODO: check timezone...
	// TODO: is [-8:PST] necessary?
	return DateTime.Now.ToString("yyyyMMddhhmmss.000");
	
}

Open in new window

0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Roshan DavisCommented:
also use Regex (namespace is System.Text.RegularExpressions) instead of Pattern.
0
ndonhauserAuthor Commented:
Thank you very much again, it is now all converted!

Thanks.
0
Roshan DavisCommented:
cool! :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.