Solved

NullReferenceException w/ authorize.net

Posted on 2004-08-30
5
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā€¦
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:
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

756 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