I have some code that consumes a webservice. Rather than retrieve a session key from the service each time, I want to try the response with a session variable I set, and if I get a 403 error get a new session key and try again.
In my limited mind nesting try/catch statements makes sense but I'm just good enough to realize there has to be a better way. I've read a few posts here mentioning things like lambda expressions with using statements and Helpers, but these may as well be gifts from an alien interplanetary science to me.
Always willing to learn I was wondering if anyone has suggestions (with details or links to details)? Everything here works, I just need a better way than nesting try statements (please and thank you). As always, you guys rock!
public string getMySessionkey goes to the server with login credentials to get a session key (required for all other inquiries) if there isn't a session key already set
searchPacket = stringbuilder with parameters and session key
if (Session["MySessionKey"] != null)
{
sessionKee = Session["MySessionKey"].ToString();
}
else
{
sessionKee = getMySessionkey(_cbE, _cbP);
System.Web.HttpContext.Current.Session.Add("MySessionKey", sessionKee);
}
try
{
mySearchResults = getResults(searchPacket.ToString());
vbResultz += Server.HtmlEncode(mySearchResults) ;
}
catch (WebException wx)
{
HttpWebResponse webresponse ;
webresponse = (HttpWebResponse)wx.Response;
switch (webresponse.StatusCode)
{
case HttpStatusCode.InternalServerError:
...
break;
case HttpStatusCode.Forbidden: // 403
vbResultz = "You aint got no valid session key!";
// code here to get a new session key and try again
break;
default:
throw;
}
}