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;
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);
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;
}
}
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;
}
byteArray = new byte[(int)length];
int offset = 0;
int numRead = 0;
while (offset < byteArray.Length && (numRead = br.Read(byteArray, offset, (byteArray.Length - offset))) >= 0)
{
offset += numRead;
}
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));
}