Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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."));
            }
        }

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

C#
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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900