Solved

NullReferenceException w/ authorize.net

Posted on 2004-08-30
5
234 Views
Last Modified: 2013-11-29
getting a NullReferenceException error on this line: myWriter.Close();

in this code:

public static string CreditCardProcessorCaller(string[] arrCCData) {

string strResult = "";
string strPost = "x_login=" + "someuser" +
"&x_password=" + "somepassword" +
"&x_version=3.1" +
"&x_delim_data=TRUE" +
"&x_delim_char=|" +
"&x_method=CC" +
"&x_recurring_billing=NO" +
"&x_type=AUTH_CAPTURE" +
"&x_amount=5.00" +
"&x_card_num=" + arrCCData[0] +
"&x_exp_date=" + arrCCData[1] +
"&x_card_code=" + arrCCData[2] +
"&x_cust_id=" + arrCCData[3] +
"&x_customer_ip=" + arrCCData[4] +
"&x_first_name=" + arrCCData[5] +
"&x_last_name=" + arrCCData[6] +
"&x_address=" + arrCCData[7] +
"&x_city=" + arrCCData[8] +
"&x_state=" + arrCCData[9] +
"&x_zip=" + arrCCData[10];

StreamWriter myWriter = null;
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create("https://security.authorize.net/gateway/transact.dll");
objRequest.Method = "POST";
objRequest.ContentLength = strPost.Length;
objRequest.ContentType = "application/x-www-form-urlencoded";

try
{
myWriter = new StreamWriter(objRequest.GetRequestStream());
myWriter.Write(strPost);
}
catch (Exception strException)
{
return strException.ToString();
}
finally
{
myWriter.Close();
}

HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
strResult = sr.ReadToEnd();
sr.Close();
}

return strResult;
}
0
Comment
Question by:loyaliser
5 Comments
 
LVL 33

Expert Comment

by:raterus
Comment Utility
I'd have to say that this line is failing in the first place..
myWriter = new StreamWriter(objRequest.GetRequestStream());

if myWriter can't be instantiated, it surely can't be closed!
0
 
LVL 1

Author Comment

by:loyaliser
Comment Utility
actually, just figured it out... typo... sending to the wrong URL:

("https://security.authorize.net/gateway/transact.dll");

should be:

("https://secure.authorize.net/gateway/transact.dll");
0
 
LVL 10

Expert Comment

by:jnhorst
Comment Utility
Also, if the fixed URL does not solve the problem, I think you should change:

HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create({url});

to:

WebRequest req = HttpWebRequest.Create({url});

Johhn
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
Comment Utility
PAQed, with points refunded (500)

Computer101
E-E Admin
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now