Click here to Skip to main content
15,883,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using an API in our portal for money transferring. Like i want to send 20k, then it will go in 4 parts, 5k in each API call, from the API company they said we have to set 3 mins for each one request. Here is my code how i set timeout, but it still shows error when 3 mins up and second request not completed.

decimal transferAmount = 0;
decimal _amount = 20000;
while (_amount > 0)
{
if (_amount > 5000)
{ transferAmount = 5000; }
else
{ transferAmount = _amount }
string webAddr = "https://www.test.in/transfer";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
httpWebRequest.ContentType = "application/json; charset=utf-8";
httpWebRequest.Method = "POST";
httpWebRequest.Timeout = 180000;

string json = "";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
json =
"{\"token\":\"123456",\"accesskey\":\"test123",\"request\": {\"amount\": \"" + _amount;
json = json + "\"}}";

streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
string _resultFinal = "";
var responseText = "";
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
responseText = streamReader.ReadToEnd();
XmlReader xReader = XmlReader.Create(new StringReader(responseText));

while (xReader.Read())
{
switch (xReader.NodeType)
{
case XmlNodeType.Element:
if (xReader.Name == "xml" || xReader.Name == "bank")
break;
else
_resultFinal = _resultFinal + xReader.Name;
break;
case XmlNodeType.Text:
_resultFinal = _resultFinal + " = " + xReader.Value + ", ";
break;
}
}
xReader.Close();
streamReader.Close();
}
httpResponse.Close();
httpResponse.Dispose();
httpWebRequest.Abort();


_amount = _amount- transferAmount;
}

What I have tried:

Please see my code and suggest me, where i am wrong and correct me sir
Posted
Comments
F-ES Sitecore 23-Jun-17 9:38am    
Are you sure they didn't mean three minutes between requests? If an API took three minutes to make a transfer I'd find a different provider.
Anand Saini 23-Jun-17 9:48am    
3 mins is the maximum minutes of response, i 99% cases in respond only in 5-10 secs but in 1% case it takes more time, in that 1% cases i am asking this issue.
F-ES Sitecore 23-Jun-17 10:00am    
usde a try\catch block to capture when a timeout has been raised and handle it accordingly.
Anand Saini 23-Jun-17 10:03am    
it goes time time when 3 mins up means 180 seconds completed.
i don't know how to check and handle it that's why asking sir.
F-ES Sitecore 23-Jun-17 10:25am    
Change

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

to

HttpWebResponse httpResponse = null;

try
{
httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
}
catch (System.Net.WebException exp)
{
System.Diagnostics.Debug.WriteLine("Error - " + exp.Message);
}

if (httpResponse == null)
{
// handle the error in an appropriate way
}

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