Заголовки HTTP одинаковы для вызовов служб ASMX и WCF? - PullRequest
0 голосов
/ 17 февраля 2011

Мы пытаемся использовать стороннюю защищенную службу WCF программно. Я пытаюсь программно добавить параметр запроса мыла и заголовки HTTP. Есть ли обязательные или стандартные пары строка / значение, которые нужно добавить в заголовок? В настоящее время мы делаем что-то вроде создания пользовательского запроса на мыло:

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;
}

Мы делаем то же самое для сервисов ASMX. Но не уверен, что это сработает и для WCF. Хотя я попытался запустить этот код, но получил 500 ошибок сервера (не уверен, связано ли это с неправильной информацией заголовка) в строке HttpWebResponse httpRes = (HttpWebResponse) httpReq.GetResponse ();

Я не очень уверен, смог ли я правильно сформулировать свою проблему. Пожалуйста, дайте мне знать, если вам нужна дополнительная информация.

...