private void InitConnection(Uri hosturl, string username, string password, string locale, bool allowUntestedVersion, string geoRestUrl, string geoRestConfigPath) { if (!string.IsNullOrEmpty(geoRestUrl)) { _geoRestConn = new GeoRestConnection(geoRestUrl, geoRestConfigPath); } m_reqBuilder = new RequestBuilder(hosturl, locale); mAnonymousUser = (username == "Anonymous"); _cred = new NetworkCredential(username, password); string req = m_reqBuilder.CreateSession(); m_username = username; m_password = password; try { this.RestartSession(); } catch (Exception ex) { throw new Exception("Failed to connect, please check network connection and login information.\nExtended error info: " + NestedExceptionMessageProcessor.GetFullMessage(ex), ex); } if (!allowUntestedVersion) ValidateVersion(this.SiteVersion); m_username = username; m_password = password; }
/// <summary> /// Restarts the server session, and creates a new session ID /// </summary> /// <param name="throwException">If set to true, the call throws an exception if the call failed</param> /// <returns>True if the creation succeed, false otherwise</returns> protected override bool RestartSessionInternal(bool throwException) { if (m_username == null || m_password == null) if (throwException) throw new Exception("Cannot recreate session, because connection was not opened with username and password"); else return false; Uri hosturl = new Uri(m_reqBuilder.HostURI); string locale = m_reqBuilder.Locale; string oldSessionId = m_reqBuilder.SessionID; try { RequestBuilder reqb = new RequestBuilder(hosturl, locale); WebClient wc = new WebClient(); wc.Credentials = new NetworkCredential(m_username, m_password); string req = reqb.CreateSession(); try { reqb.SessionID = System.Text.Encoding.Default.GetString(wc.DownloadData(req)); if (reqb.SessionID.IndexOf("<") >= 0) throw new Exception("Invalid server token recieved: " + reqb.SessionID); else CheckAndRaiseSessionChanged(oldSessionId, reqb.SessionID); } catch (Exception ex) { reqb.SessionID = null; bool ok = false; try { //Retry, and append missing path, if applicable if (!hosturl.ToString().EndsWith("/mapagent/mapagent.fcgi")) { string tmp = hosturl.ToString(); if (!tmp.EndsWith("/")) tmp += "/"; hosturl = new Uri(tmp + "mapagent/mapagent.fcgi"); reqb = new RequestBuilder(hosturl, locale); req = reqb.CreateSession(); reqb.SessionID = System.Text.Encoding.Default.GetString(wc.DownloadData(req)); if (reqb.SessionID.IndexOf("<") >= 0) throw new Exception("Invalid server token recieved: " + reqb.SessionID); ok = true; } } catch {} if (!ok) { if (throwException) //Report original error { if (ex is WebException) //These exceptions, we just want the underlying message. No need for 50 bajillion nested exceptions throw; else //We don't know what this could be so grab everything throw new Exception("Failed to connect, perhaps session is expired?\nExtended error info: " + ex.Message, ex); } else return false; } else { CheckAndRaiseSessionChanged(oldSessionId, reqb.SessionID); } } //Reset cached items lock (SyncRoot) { m_siteVersion = new Version(((SiteVersion)DeserializeObject(typeof(SiteVersion), wc.OpenRead(reqb.GetSiteVersion()))).Version); m_featureProviders = null; m_cachedProviderCapabilities = null; m_reqBuilder = reqb; } return true; } catch { if (throwException) throw; else return false; } }