asp.net web api - How to use PostAsXmlAsync with FomUrlEncoding and KeyValuePairs -
i have following webapi, i'm trying find way simplify such that,
i can use postasxmlasync instead of manually serializing object (post has keyvaluepair xmldata="...
also, on handler response.content.readasasync() failing deserialize xml response server.
[httppost] [route("validatepayment")] public async task<paymentresponsedto> validatepayment(paymentrequestdto paymentrequestinfo) { var xmlserializer = new xmlserializer(paymentrequestinfo.gettype()); string transactiondata; using (var stringwriter = new stringwriter()) { xmlwritersettings xmlwritersettings = new xmlwritersettings(); xmlwritersettings.omitxmldeclaration = true; using (var xmlwriter = xmlwriter.create(stringwriter, xmlwritersettings)) { xmlserializernamespaces ns = new xmlserializernamespaces(); ns.add("", ""); xmlserializer.serialize(xmlwriter, paymentrequestinfo, ns); transactiondata = stringwriter.tostring(); } } var convergeurl = configurationmanager.appsettings["convergeurl"]; var postdata = new list<keyvaluepair<string, string>> { new keyvaluepair<string, string>("xmldata", transactiondata) }; using (var httpclient = new httpclient()) { using (var content = new formurlencodedcontent(postdata)) { content.headers.clear(); content.headers.add("content-type", "application/x-www-form-urlencoded"); httpresponsemessage responsemessage = null; try { responsemessage = await httpclient.postasync(convergeurl, content); } catch (exception) { throw; } return handlevalidatepaymentresponse(responsemessage).result; } } } private async task<paymentresponsedto> handlevalidatepaymentresponse(httpresponsemessage response) { if (response.issuccessstatuscode) { return await response.content.readasasync<paymentresponsedto>(); } else if (response.statuscode.tostring().tolower() == "unauthorized") { throw new unauthorizedaccessexception("webservice login failure."); } else if (response.reasonphrase.tolower() == "not found") { return default(paymentresponsedto); } else { throw new exception(response.reasonphrase); } } }
Comments
Post a Comment