Link to home
Start Free TrialLog in
Avatar of loyaliser
loyaliserFlag for United States of America

asked on

NullReferenceException w/ authorize.net

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;
}
Avatar of raterus
raterus
Flag of United States of America image

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!
Avatar of loyaliser

ASKER

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");
Avatar of jnhorst
jnhorst

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
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial