Sunday, February 13, 2011

Calling authenticated WSE web service in Visual Studio 2008.

Hi All,

This time i was given a task to call a web service which was written in older technology WSE and as you all know that its obsolete and replaced by WCF services but still some old written services are to be used.

When i added the service reference in my application it auto generates the configuration file as shown below, but by using this configuration file i was not able to provide credentials to web service call.

<basicHttpBinding>
        <binding name="FsetServiceSoap" closeTimeout="00:01:00" openTimeout="00:01:00"
          receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
          bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
          maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
          messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
          useDefaultWebProxy="true">

          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
            maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="Transport">
            <transport clientCredentialType="None" proxyCredentialType="None"
              realm="">
              <extendedProtectionPolicy policyEnforcement="Never" />
            </transport>
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>


        </binding>
        <binding name="FsetServiceSoap1" closeTimeout="00:01:00" openTimeout="00:01:00"
          receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
          bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
          maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
          messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
          useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
            maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None" realm="">
              <extendedProtectionPolicy policyEnforcement="Never" />
            </transport>
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>


so i replaced this basichttp binding with custom binding.


<customBinding>

        <binding name="FsetServiceSoap" >

          <textMessageEncoding messageVersion="Soap12WSAddressingAugust2004" />

          <security authenticationMode="UserNameOverTransport"></security>

          <httpsTransport/>

        </binding>

      </customBinding>

and when calling the web service passed the credentials and it worked like charm.

FsetServiceHeader header = new FsetServiceHeader();
FsetServiceSoapClient client = new FsetServiceSoapClient();
client.ClientCredentials.UserName.UserName = _userName;
client.ClientCredentials.UserName.Password = _password;
byte[] rawData = File.ReadAllBytes(_filePath);
TransmissionAcknowledgementType result = client.SendTransmission(header, SendType, rawData);

Hope it helps.

Happy coding.

Regards,
U

Calling OOyala player API using C#.

Hello Friends,

Some or any of you might have used OOyala tool for you projects as its one of the best video management tools i have ever seen.

Some days back i have given a task to get ooyala integrated to our project and i decided to do it via their API but all the samples given on site are eighter in PHP or in Ruby but i want things to be done in c# so by following their documentation i have finally got the response from ooyala site.

Here i am showing you two functions and two properties.

1. _params: i have used sorted dictionary because ooyala demands that all parameters should be in acceding order.

2. Query type: specify which API should be called.

3. send_request(): It does the required encoding of parameters by using the secret and partner code and generated the URL by which XML response can be fetched.

4. getXMLResponse(): Get all XML from URL.

#region Properties

        private string PARTNET_CODE = "PARTNET_CODE";
        private string PARTNET_SECRET = "PARTNET_SECRET";


        private string _queryType = string.Empty;
        public string QueryType
        {
            get
            {
                return _queryType;
            }

            set
            {
                _queryType = value;
            }

        }

        private SortedDictionary<string, string> _params;
        public SortedDictionary<string, string> Params
        {

            get
            {
                return _params;
            }
            set
            {
                _params = value;
            }

        }

        #endregion



        #region Public function



        public string getXMLResponse()
        {


            string sUrl = send_request();
            StringBuilder oBuilder = new StringBuilder();
            StringWriter oStringWriter = new StringWriter(oBuilder);
            XmlTextReader oXmlReader = new XmlTextReader(sUrl);
            XmlTextWriter oXmlWriter = new XmlTextWriter(oStringWriter);
            while (oXmlReader.Read())
            {
                oXmlWriter.WriteNode(oXmlReader, true);
            }
            oXmlReader.Close();
            oXmlWriter.Close(); 

            return oBuilder.ToString();


        }


        /// <summary>
        /// Returns the URL for request parameters 
        /// </summary>
        private string send_request()
        {

            string url = "http://api.ooyala.com/partner/" + QueryType + "?pcode=" + PARTNET_CODE;
            foreach (KeyValuePair<string, string> pair in _params)
            {
                PARTNET_SECRET += pair.Key + "=" + pair.Value;
                url += "&" + HttpUtility.UrlEncodeUnicode(pair.Key) + "=" + HttpUtility.UrlEncodeUnicode(pair.Value);
            }
            SHA256Managed sh = new SHA256Managed();
            byte[] request = System.Text.Encoding.ASCII.GetBytes(PARTNET_SECRET);
            sh.Initialize();
            byte[] b4bbuff = sh.ComputeHash(request, 0, request.Length);
            string hashString = System.Text.Encoding.ASCII.GetString(b4bbuff);

            string encryptedRequest = HttpUtility.UrlEncode((Convert.ToBase64String(b4bbuff)).Substring(0, 43));
            url += "&signature=" + encryptedRequest;

            return url;
        }
#endregion


Once you get the XMl response do what ever you want, Deserilise it and use it in c# code or parse it with jQuery choice is yours.

Hope it helps.

Happy coding.

Regards,
U