Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WCF
Hi,
 
I am creating a WCF Service in that WCF Service i am Calling a Web Service.
In the strRequestUrl i am passing Service Url.While creating the object for HttpWebResonse
i am getting Unable to Connect to to the remote Server error,anyone please tell me what is the problem.
Here strClientLoginName and strKeyPath i am taking from web.config and all other values from DB.
 
The Below is my code.
 
public void ProcessRequest(InEscrowDepositBO objInEscrowDepositBO)
        {
            try
            {
                string strSeqNumber = objInEscrowDepositBO.ClearedSeqNum;
                string strAccountNumber = objInEscrowDepositBO.BankAccount.FormattedAcctNum;
                string strPaidDate = objInEscrowDepositBO.DepositedDate.MMDDYYYY;
                string strDrawBankAccount = objInEscrowDepositBO.Payment.AccountNum;
                string strClientLoginName = WebConfigurationManager.AppSettings["ClientLoginName"];
                string strKeyPath = WebConfigurationManager.AppSettings["ClientKeyPath_file"];
                string strCheckNumber = objInEscrowDepositBO.Payment.CheckNum; //Convert.ToString(context.Request.QueryString["CheckNumber"]);
                decimal strAmount = Convert.ToDecimal(FormatCurrency(((Money)objInEscrowDepositBO.DepositAmount).getDisplayAmt()));
                string[] arrPaidDate = new string[3];
                if (strSeqNumber.Trim().Length > 9)
                {
                    int iStindex = strSeqNumber.Length - 9;
                    strSeqNumber = strSeqNumber.Substring(iStindex);
                }
                if (strPaidDate != null)
                {
                    arrPaidDate = strPaidDate.Split('/');
                    if (arrPaidDate.Length == 3)
                    {
                        strPaidDate = arrPaidDate[2] + "-" + arrPaidDate[0] + "-" + arrPaidDate[1];
                    }
                }
                string strQueryDeposit = "depositseq=" + strSeqNumber + "&account=" + strAccountNumber + "&date=" + strPaidDate;
                string HMACkeyDeposit = GetHMACKey(strKeyPath, strQueryDeposit); //HMAC key for the web method     
                if ((HMACkeyDeposit != string.Empty) && (HMACkeyDeposit != null))
                {
                    string strDepositServiceUrl = WebConfigurationManager.AppSettings["DepositServiceUrl"] + "/" + strClientLoginName + "/" + HMACkeyDeposit + "?" + strQueryDeposit;
                    string strResponse = GetDepositedCheckSeq(strDepositServiceUrl);
                    string strResult = FindDepositedCheck(strResponse, strDrawBankAccount, strCheckNumber, strAmount);
                }
            }
            catch (Exception ex)
            {
                FAFObject.LogError("Error to load WebMethod GetDepositImages : " + ex.Message, ex);
                throw new FaultException<ServiceFault>(new ServiceFault("GetDepositImages", "Internal server error."));
            }
        }
private string GetDepositedCheckSeq(string strRequestUrl)
        {
            StreamReader streamReader = null;
            HttpWebResponse httpWebResponse = null;
            string strResponse = string.Empty;
            try
            {
                Uri targetUri = new Uri(strRequestUrl);
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(strRequestUrl);
                httpWebRequest.Method = WebRequestMethods.Http.Get;
                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                if (httpWebRequest.HaveResponse && httpWebResponse.StatusCode == HttpStatusCode.OK)
                {
                    streamReader = new StreamReader(httpWebResponse.GetResponseStream());
                    strResponse = streamReader.ReadToEnd();
                    return strResponse;
                }
                //else
                //{
                //    throw new ApplicationException("Http error returned : " + httpWebResponse.StatusCode.ToString());
                //}

            }
            catch (WebException ex)
            {
                FAFObject.LogError("Error to load WebMethod GetDepositImages : " + ex.Message, ex);
                throw new FaultException<ServiceFault>(new ServiceFault("GetDepositImages", "Internal server error."));
            }
            finally
            {
                if (streamReader != null)
                    streamReader.Close();
 
                if (httpWebResponse != null)
                    httpWebResponse.Close();
            }
            return strResponse;
        }
public string GetHMACKey(string ClientKeyPath, string QueryString)
        {
            string HMAC = string.Empty;
            byte[] keyByte = GetKeyBodyFromFile(ClientKeyPath);
            if (keyByte != null)
            {
                byte[] messageBytes = Encoding.UTF8.GetBytes(QueryString);
                HMACSHA1 hmac = new HMACSHA1(keyByte);
                byte[] hmacHash = hmac.ComputeHash(messageBytes);
                byte[] hexBytes = ToHex(hmacHash);
                HMAC = Encoding.UTF8.GetString(hexBytes);
            }
            return HMAC;
        }
        private byte[] GetKeyBodyFromFile(string ClientKeyPath)
        {
            byte[] byteArray = null;
 
            FileInfo fiKey = new FileInfo(ClientKeyPath);
            if (!fiKey.Exists)
            {
                return byteArray;
            }
            using (BinaryReader br = new BinaryReader(fiKey.OpenRead()))
            {
                long length = fiKey.Length;
                if (length > int.MaxValue)
                {
                    return byteArray;
                }
                // Create the byte array to hold the data
                byteArray = new byte[(int)length];
                // Read in the bytes
                int offset = 0;
                int numRead = 0;
                while (offset < byteArray.Length && (numRead = br.Read(byteArray, offset, (byteArray.Length - offset))) >= 0)
                {
                    offset += numRead;
                }
                // Ensure all the bytes have been read in
                if (offset < byteArray.Length)
                {
                    byteArray = null;
                    return byteArray;
                }
            }
            return byteArray;
        }
        private byte[] ToHex(byte[] data)
        {
            byte[] hex = new byte[data.Length * 2];
            for (int i = 0; i < data.Length; i++)
            {
                byte lowNibble = (byte)(data[i] & 0xF);
                byte highNibble = (byte)((data[i] >> 4) & 0xF);
                hex[i * 2] = ToHex(highNibble);
                hex[i * 2 + 1] = ToHex(lowNibble);
            }
            return hex;
        }
        private byte ToHex(byte Nibble)
        {
            if (Nibble > 15)
            {
                return Convert.ToByte(-1);
            }
            if (Nibble < 10)
            {
                return (byte)(0x30 + Nibble);
            }
            return (byte)('a' + (Nibble - 10));
        }
        
Posted 15-Feb-13 23:54pm
Ashad25198

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 240
1 Kamal Rocks 184
2 PIEBALDconsult 150
3 BillWoodruff 148
4 Jochen Arndt 135
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 16 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100