Skip to content

Commit 5affebc

Browse files
committed
Cleaning up the web request and response
1 parent 3725728 commit 5affebc

File tree

1 file changed

+33
-36
lines changed

1 file changed

+33
-36
lines changed

Authorize.NET/Util/HttpUtility.cs

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -47,70 +47,67 @@ public static ANetApiResponse PostData<TQ, TS>(AuthorizeNet.Environment env, TQ
4747
webRequest.Proxy = SetProxyIfRequested(webRequest.Proxy);
4848

4949
var requestType = typeof (TQ);
50-
5150
var serializer = new XmlSerializer(requestType);
5251
using (var writer = new XmlTextWriter(webRequest.GetRequestStream(), Encoding.UTF8))
5352
{
5453
serializer.Serialize(writer, request);
5554
}
5655

5756
// Get the response
58-
String stringResponse = null;
57+
String responseAsString = null;
5958
Logger.debug(string.Format("Retreiving Response from Url: '{0}'", postUrl));
6059
using (var webResponse = webRequest.GetResponse())
6160
{
6261
Logger.debug(string.Format("Received Response: '{0}'", webResponse));
6362

64-
using (var streamData = webResponse.GetResponseStream())
63+
using (var responseStream = webResponse.GetResponseStream())
6564
{
66-
if (null != streamData)
65+
if (null != responseStream)
6766
{
68-
using (var reader = new StreamReader(streamData))
67+
using (var reader = new StreamReader(responseStream))
6968
{
70-
stringResponse = reader.ReadToEnd();
69+
responseAsString = reader.ReadToEnd();
7170
}
72-
Logger.debug(string.Format("Response from Stream: '{0}'", stringResponse));
71+
Logger.debug(string.Format("Response from Stream: '{0}'", responseAsString));
7372
}
7473
}
7574
}
76-
if (null != stringResponse)
75+
if (null != responseAsString)
7776
{
78-
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(stringResponse)))
77+
using (var memoryStreamForResponseAsString = new MemoryStream(Encoding.UTF8.GetBytes(responseAsString)))
7978
{
8079
var responseType = typeof (TS);
8180
var deSerializer = new XmlSerializer(responseType);
8281

83-
//if (null != stringResponse)
82+
Object deSerializedObject;
83+
try
8484
{
85-
Object deSerializedObject;
86-
87-
try
88-
{
89-
deSerializedObject = deSerializer.Deserialize(stream);
90-
}
91-
catch (Exception)
92-
{
93-
stream.Seek(0, SeekOrigin.Begin);
94-
var errorDeserializer = new XmlSerializer(typeof (ANetApiResponse));
95-
deSerializedObject = errorDeserializer.Deserialize(stream);
96-
}
85+
// try deserializing to the expected response type
86+
deSerializedObject = deSerializer.Deserialize(memoryStreamForResponseAsString);
87+
}
88+
catch (Exception)
89+
{
90+
// probably a bad response, try if this is an error response
91+
memoryStreamForResponseAsString.Seek(0, SeekOrigin.Begin); //start from beginning of stream
92+
var genericDeserializer = new XmlSerializer(typeof (ANetApiResponse));
93+
deSerializedObject = genericDeserializer.Deserialize(memoryStreamForResponseAsString);
94+
}
9795

98-
//if error response
99-
if (deSerializedObject is ErrorResponse)
96+
//if error response
97+
if (deSerializedObject is ErrorResponse)
98+
{
99+
response = deSerializedObject as ErrorResponse;
100+
}
101+
else
102+
{
103+
//actual response of type expected
104+
if (deSerializedObject is TS)
100105
{
101-
response = deSerializedObject as ErrorResponse;
106+
response = deSerializedObject as TS;
102107
}
103-
else
108+
else if (deSerializedObject is ANetApiResponse) //generic response
104109
{
105-
//actual response of type expected
106-
if (deSerializedObject is TS)
107-
{
108-
response = deSerializedObject as TS;
109-
}
110-
else if (deSerializedObject is ANetApiResponse) //generic response
111-
{
112-
response = deSerializedObject as ANetApiResponse;
113-
}
110+
response = deSerializedObject as ANetApiResponse;
114111
}
115112
}
116113
}
@@ -135,7 +132,7 @@ public static IWebProxy SetProxyIfRequested(IWebProxy proxy)
135132
{
136133
newProxy = new WebProxy(proxyUri);
137134
}
138-
if (null != newProxy)
135+
//if (null != newProxy)
139136
{
140137
newProxy.UseDefaultCredentials = true;
141138
newProxy.BypassProxyOnLocal = true;

0 commit comments

Comments
 (0)