We are trying to consume 3rd party secured WCF service programatically. I am trying to programatically add the soap request parameter and the HTTP Headers. Is there a mandatory or standard string/value pairs to be added to the header? Currently we are doing something like to build a custom soap request:
public static string CustomSoapReq(string WSDL_URL)
{
string strCustomModels = string.Empty;
try
{
System.Net.Cookie SMCookie;
CookieCollection IFCookies;
string successfuluserid = String.Empty;
ClsGFWUtility clsutill;
HttpWebRequest httpReq;
SMCookie = new Cookie();
IFCookies = new CookieCollection();
clsutill = new ClsGFWUtility(); //inbuilt function to get the cookie information
IFCookies = clsutill.GetCookie("LogonUrl", "IFLoginUserId", "IFLoginPassword", out successfuluserid);
if (IFCookies != null && successfuluserid.Length > 0)
{
WebRequest wReq = WebRequest.Create(WSDL_URL);
httpReq = (HttpWebRequest)wReq;
httpReq.Timeout = 99999;
httpReq.ReadWriteTimeout = 99999;
httpReq.ServicePoint.MaxIdleTime = 99999;
httpReq.AllowWriteStreamBuffering = true;
XmlDocument xsldoc = new XmlDocument();
StringBuilder soapListReq = new StringBuilder();
soapListReq.Append("<?xml version='1.0' encoding='UTF-8'?> ");
soapListReq.Append("<soap:Envelope ");
soapListReq.Append(" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>");
soapListReq.Append("<soap:Body>");
soapListReq.Append("<GetCashFlow xmlns='TestProject.Services'>");
soapListReq.Append("<inputparam>");
soapListReq.Append("<ac_unq_ids xmlns='TestProject.Application.wsPMCData.Cashflow'>");
soapListReq.Append("<ac_unq_id>1234567890</ac_unq_id>");
soapListReq.Append("</ac_unq_ids>");
soapListReq.Append("<period_from xmlns='TestProject.Application.wsPMCData.Cashflow'>2008-05-01</period_from>");
soapListReq.Append("<period_to xmlns='TestProject.Application.wsPMCData.Cashflow'>2010-08-31</period_to>");
soapListReq.Append("<selected_curr xmlns='TestProject.Application.wsPMCData.Cashflow'>USD</selected_curr>");
soapListReq.Append("<report_type xmlns='TestProject.Application.wsPMCData.Cashflow'>I</report_type>");
soapListReq.Append("</inputparam>");
soapListReq.Append("</GetCashFlow>");
soapListReq.Append("</soap:Body>");
soapListReq.Append("</soap:Envelope>");
//Add Soap Header
httpReq.Headers.Add("SOAPAction", "http://tempuri.org/GetCashFlowDetail");
httpReq.Headers.Add("Version", "1.0");
httpReq.Headers.Add("OnBehalfOf", "");
httpReq.Headers.Add("Role", "1");
httpReq.Headers.Add("EndPoint", "WSHttpBinding_IwsPMCData");
//Add Http Headers
httpReq.Headers.Add("Version", "1.0");
httpReq.Headers.Add("OnBehalfOf", "");
httpReq.Headers.Add("Role", "1");
httpReq.Headers.Add("EndPoint", "WSHttpBinding_IwsPMCData");
httpReq.Headers.Add("ServiceId", "001");
httpReq.Headers.Add("DateTime", DateTime.Now.ToString("MM/dd/yyyy"));
httpReq.Headers.Add("ClientApplication", "WSHttpBinding_IwsPMCData");
httpReq.Headers.Add("TraceWebMethod", "false");
httpReq.Headers.Add("ClientTouchPoint", "ClientTouchPoint");
httpReq.Headers.Add("ChannelInfo", "ChannelInfo");
httpReq.Headers.Add("Environment", "WINOS");
httpReq.Headers["http_tmsamlsessionticket"] = string.Empty;
SMCookie = IFCookies["SMSESSION"];
httpReq.Headers.Add("MSMUSER", successfuluserid);
httpReq.Headers.Add("MSMSESSIONID", SMCookie.Value);
httpReq.Headers.Add("http_tmsamlsessionticket", "");
httpReq.Headers.Add("AcctGroupID", "");
httpReq.Headers.Add("AcctGroupType", "");
httpReq.Headers.Add("ExternalContext", "");
httpReq.Headers.Add("MSAML_ASSERTION", "");
string sSMCookie = "SMSESSION=" + SMCookie.Value;
httpReq.Headers["Cookie"] = sSMCookie;
httpReq.Method = "POST";
httpReq.ContentType = "text/xml; charset=utf-8";
httpReq.ContentLength = soapListReq.ToString().Length;
Stream StreamGoingtoWebservice;
StreamGoingtoWebservice = httpReq.GetRequestStream();
ASCIIEncoding ascEnc = new ASCIIEncoding();
Byte[] byteArray = ascEnc.GetBytes(soapListReq.ToString());
StreamGoingtoWebservice.Write(byteArray, 0, byteArray.Length);
StreamGoingtoWebservice.Close();
HttpWebResponse httpRes = (HttpWebResponse)httpReq.GetResponse();
if (httpRes.StatusCode == HttpStatusCode.OK)
{
StreamReader strmRdr = new StreamReader(httpRes.GetResponseStream());
//XmlReader xr = XmlReader.Create(strmRdr);
strCustomModels = strmRdr.ReadToEnd();
strmRdr.Close();
}
}
}
catch (Exception ex)
{
throw;
}
return strCustomModels;
}
We do the similar thing for ASMX services. But not sure if this will work for WCF aswell. Although, I tried running this code but getting 500 server error (not sure if it has to do with wrong header info) at line HttpWebResponse httpRes = (HttpWebResponse)httpReq.GetResponse();
I not very sure if I was able to frame my problem correctly. Please let me know if you need more information.