class XmlRequests
{
public static XDocument getLoginRequest()
{
string reqXml = @"
XDocument reqXML = XDocument.Parse(reqXml);
return reqXML;
}
am required to call a webservice api but since its webmethods are not visible when added as i web reference i have to call the webservice dynamically.
from an aspx page code behind i have the following code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using System.IO;
using System.Net;
namespace AirtimeTransfer
{
public partial class AgentLogin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
XDocument reqXML = XmlRequests.getLoginRequest();
string uri = "https://testapi.company.com:port/agent";
string contentType = "text/xml;charset=UTF-8";
string soapAction = "AgentLogin";
/*Calling method topUp whatever*/
XDocument resp = new XDocument();
resp = CallSoapServiceInternal(uri, soapAction, contentType, reqXML);
XDocument XMLDoc = new XDocument(resp);
XElement sessionId = (from xml2 in XMLDoc.Descendants("sessionToken")
select xml2).FirstOrDefault();
Response.Write(sessionId);
}
private XDocument CallSoapServiceInternal(string uri, string soapAction, string contentType, XDocument reqXML)
{
var req = (HttpWebRequest)WebRequest.Create(uri);
req.ContentType = contentType;
req.Method = "POST";
req.Headers.Add("SOAPAction", soapAction);
req.Accept = "text/xml";
//req.Credentials = CredentialCache.DefaultCredentials;
req.Timeout = 20000;
//req.Timeout = System.Threading.Timeout.Infinite;
/* using (var reqStream = req.GetRequestStream())
{
reqXml.Save(reqStream);
}
*/
using (Stream stm = req.GetRequestStream())
{
using (StreamWriter stmw = new StreamWriter(stm))
{
stmw.Write(reqXml);//says that reqXml does not exixt in the current context
}
}
string respStr = "";
try
{
using (var resp = (HttpWebResponse)req.GetResponse())
{
using (var rdr = new StreamReader(resp.GetResponseStream()))
{
respStr = rdr.ReadToEnd();
}
}
}
catch (Exception ex)
{
//throw new Exception("Error getting service response.", ex);
throw ex;
}
Response.Write(respStr);
//Assert.IsTrue(respStr.Length > 0, "Nothing returned");
XDocument respXml = new XDocument();
try
{
respXml = XDocument.Parse(respStr);
}
catch (Exception e)
{
throw e;
}
return respXml;
}
}
}
what might i be doing wrong and if possible can some tell me of an easier way to do this?
VulpesPosted Nov 6, 2014, 6:24 AM
Kelvin hassanPosted Nov 6, 2014, 5:38 AM
VulpesPosted Nov 6, 2014, 5:28 AM
private XDocument CallSoapServiceInternal(string uri, string soapAction, string contentType, XDocument reqXml)
Kelvin hassanPosted Nov 6, 2014, 5:06 AM
Riddhi ValechaPosted Nov 6, 2014, 4:26 AM
At which line (when you debug the code) this error occours ??