Solved

NullReferenceException w/ authorize.net

Posted on 2004-08-30
5
235 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
ID: 11933900
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
ID: 11934037
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
ID: 11934580
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
ID: 12185365
PAQed, with points refunded (500)

Computer101
E-E Admin
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Read about how to choose the best possible content marketing agency to suit your needs. Content marketing has become an integral part of running a successful tech business, so it is wise to be informed.
How important is it to take extra precautions to protect your online business? These are some steps you can take to make sure you're free of any cyber crime.
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:
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

863 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

22 Experts available now in Live!

Get 1:1 Help Now