public BluePayPost20ResponseV3 Process(BluePayMessage msg) { try { var req = _webRequestProvider(_endpoint); using (var writer = new StreamWriter(req.GetRequestStream())) { var isFirst = true; foreach (var kvp in msg.GetFields()) { if (!isFirst) { writer.Write("&"); } else { isFirst = false; } writer.Write(HttpUtility.UrlEncode(kvp.Key)); writer.Write('='); writer.Write(HttpUtility.UrlEncode(kvp.Value?.ToString())); } } var response = req.GetResponse(); using (var reader = new StreamReader(response.GetResponseStream())) { return(_parser.Parse(reader)); } } catch (WebException e) { using (var response = e.Response) { if (response == null) { throw; } using (var stream = response.GetResponseStream()) { if (stream == null) { throw new BluePayException(e.Message, e); } ; using (var reader = new StreamReader(stream)) { return(_parser.Parse(reader)); } } } } }
public async Task <BluePayPost20ResponseV3> ProcessAsync(BluePayMessage msg) { try { var req = _webRequestProvider(_endpoint); using (var writer = new StreamWriter(req.GetRequestStream())) { var delimeter = ""; foreach (var kvp in msg.GetFields()) { var key = HttpUtility.UrlEncode(kvp.Key); var value = HttpUtility.UrlEncode(kvp.Value?.ToString()); await writer.WriteAsync($"{delimeter}{key}={value}"); delimeter = "&"; } } var response = await req.GetResponseAsync(); using (var reader = new StreamReader(response.GetResponseStream())) { return(await _parser.ParseAsync(reader)); } } catch (WebException e) { using (var response = e.Response) { if (response == null) { throw; } using (var stream = response.GetResponseStream()) { if (stream == null) { throw new BluePayException(e.Message, e); } ; using (var reader = new StreamReader(stream)) { return(await _parser.ParseAsync(reader)); } } } } }