בעיה בקבלת שערים מבנק ישראל
-
בעיה בקבלת שערים מבנק ישראל
יש לי קוד של קבלת שערים מבנק ישראל שכבר עובד הרבה שנים.
מיום חמישי יש לו שגיאה שמשהו חסום,
האם עוד משהי נתקלה בבעיה?
הבעיה בשורה הזו:
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
הוא כותב:
The underlying connection was closed: The connection was closed unexpectedly.’
להלן הקוד…
<pre style=”direction: ltr; ” class=””>
public Decimal ShekelDollarRate(DateTime RateDate)
{
// retrieve current ILS-USD rate from Bank of Israel (expects URL to be supplied in form like https://www.boi.org.il/currency.xml?rdate={0:yyyyMMdd}&curr=01)
String BOIURL = String.Format((String)”https://www.boi.org.il/currency.xml?rdate={0:yyyyMMdd}&curr=01″, RateDate);
// XmlDocument for results
XmlDocument shekelDollarFile = new XmlDocument();
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
System.Net.HttpWebRequest request = System.Net.WebRequest.Create(BOIURL) as HttpWebRequest;
request.CookieContainer = new CookieContainer();
request.AllowAutoRedirect = false;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
int waitcount = 0;
while ((new[] {“MultipleChoices”, “Ambiguous”,
“MovedPermanently”, “Moved”,
“Found”, “Redirect”,
“SeeOther”, “RedirectMethod”,
“TemporaryRedirect”, “RedirectKeepVerb”}
).Contains(response.StatusCode.ToString())
&& (response.Headers.AllKeys.Contains(“Location”) ? response.Headers.Get(“Location”) : “”) == (new System.Uri(BOIURL)).PathAndQuery
&& waitcount < 10
)
{
// we were redirected to the same place, so have another go
// but this time we will use a new request and send the cookie found in the last CookieContainer in the request headers
// save old cookie container, if present; it should contain a cookie we were given
CookieContainer RequestCookieContainer = null;
if (request.CookieContainer != null) { RequestCookieContainer = request.CookieContainer; }
// create a new clean request object
request = System.Net.WebRequest.Create(BOIURL) as HttpWebRequest;
request.AllowAutoRedirect = false;
// add the cookie the server expects to get from us this time
if (RequestCookieContainer != null)
{
CookieCollection Cookies = RequestCookieContainer.GetCookies(new Uri(BOIURL));
if (Cookies.Count >= 1)
{
request.Headers.Add(“Cookie”, Cookies[0].Name + “=” + Cookies[0].Value);
}
}
response = request.GetResponse() as HttpWebResponse; // try getting the page again
waitcount++; // increment retry count
}
if (response.StatusCode != HttpStatusCode.OK)
{
throw new Exception(“Encountered error while retrieving BOI rates.\r\n\r\n” + String.Format(“Code: {0}\r\nError: {1}”, response.StatusCode.ToString(), response.StatusDescription + “\r\n” + String.Format(“ResponseUri: {0}\r\nLocation: {1}”, response.ResponseUri, (response.Headers.AllKeys.Contains(“Location”) ? response.Headers.Get(“Location”) : “”))) /*, we */);
}
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(responseStream))
{
shekelDollarFile.LoadXml(reader.ReadToEnd());
reader.Close();
}
}
Decimal ShekelDollarRate = 0.0M;
if (shekelDollarFile.SelectNodes(“/CURRENCIES/CURRENCY”).Count > 0)
{
if (!Decimal.TryParse(shekelDollarFile.SelectSingleNode(“/CURRENCIES/CURRENCY[1]/RATE”).InnerText, out ShekelDollarRate))
{
throw new Exception(“Encountered error while parsing BOI rates.\r\n\r\n” + shekelDollarFile.OuterXml);
}
}
return ShekelDollarRate;
}
Log in to reply.